Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 用户表单中的组合框问题_Excel_Vba_Userform - Fatal编程技术网

Excel 用户表单中的组合框问题

Excel 用户表单中的组合框问题,excel,vba,userform,Excel,Vba,Userform,我在用户表单中工作,并在组合框上编码。我使用.additem在组合框中放置了一个下拉列表,每次用户按下列表中的一个项目时,都会出现一个消息框 出于某种原因,我的第一个代码使消息框重新出现在组合框的下一行,因此当按下第二行时,我有两个消息框,而不是一个 是因为和功能吗?还有别的办法吗 为了澄清这一点,ComboBox1.ListIndex=2(显示2个消息框,而我只需要我已编码的消息框)和ComboBox1.ListIndex=3(显示3个消息框,而不是1个) 和运算符(非函数)不会短路1,因此为

我在用户表单中工作,并在组合框上编码。我使用
.additem
在组合框中放置了一个下拉列表,每次用户按下列表中的一个项目时,都会出现一个消息框

出于某种原因,我的第一个代码使消息框重新出现在组合框的下一行,因此当按下第二行时,我有两个消息框,而不是一个

是因为
功能吗?还有别的办法吗

为了澄清这一点,
ComboBox1.ListIndex=2
(显示2个消息框,而我只需要我已编码的消息框)和
ComboBox1.ListIndex=3
(显示3个消息框,而不是1个)

运算符(非函数)不会短路1,因此为了评估布尔表达式结果是否为
,VBA需要
MsgBox
函数的结果。。。对于每种情况

'both foo and bar need to be evaluated to know whether DoSomething needs to run:
If foo And Bar Then DoSomething
使
MsgBox
调用有条件-我建议使用
Select Case
块,以避免每次重复
ComboBox1.ListIndex
成员访问:

Select Case ComboBox1.ListIndex
   Case 1
       If Msgbox("Do you want to create a new company?", vbYesNo) = vbYes Then UserForm1.Show
   Case 2
       If Msgbox("Do you want to open the reports screen?", vbYesNo) = vbYes Then UserForm2.Show
   Case 3
       If Msgbox("Are you sure", vbYesNo) = vbYes Then Unload AccountsVbaPro
End Select
请注意,
UserForm1.Show
/
UserForm2.Show
最终是,如果该代码位于名为
AccountsVbaPro
的表单的代码后面,则卸载AccountsVbaPro
也是如此


1 VBA中不存在短路运算符。例如,在VB.NET中,您可以使用
以及
OrElse
操作符,这两个操作符都可以。短路逻辑运算符的结果是,一旦布尔表达式的结果已知,对其求值就可以退出:

If True Or True Or False Then ' all operands need to be evaluated
vs


哇,真管用,马修·金登,谢谢你。我对编程完全陌生,所以我不知道您可以使用(选择案例)。至于UserForm1.Show/UserForm2.Show可能会产生问题的注意事项,这些问题何时会发生?当您开始
New
ing填充时。见链接文章;-)
If True Or True Or False Then ' all operands need to be evaluated
If True OrElse True OrElse False Then ' evaluation stops at the first True; 2nd & 3rd operands are skipped