简体   繁体   English

Excel VBA-组合框

[英]Excel VBA- Combobox

Hi am a complete noob in programming and VBA. 大家好,我是编程和VBA的完全入门者。 I have created a userform and added a few comboboxes. 我创建了一个用户窗体,并添加了一些组合框。 Within the comboboxes I have created a drop down list using the With .additem EndWith function. 在组合框中,我使用With .additem EndWith函数创建了一个下拉列表。 Now every time a user presses an item within a combobox i have inserted message boxes by using codes such as this 现在,每次用户按下组合框内的项目时,我都会使用诸如此类的代码插入消息框

if ComboBox1.ListIndex = 2 Then Msgbox "Do you want to open the Reports screen?", vbYesNo 

The code Works but sometimes the user must press the button several times make the MsgBox appear. 该代码有效,但有时用户必须多次按下按钮才能显示MsgBox。

I have now created a new userform and have tried to make the following 我现在创建了一个新的用户表单,并尝试进行以下操作

if ComboBox1.ListIndex = 1 Msgbox msg("Do you want to create a new company?", vbYesNo) = vbYes then userform1.show 

The above code doesn't work but due to my lack of knowledge i don't know what else to do that's why i am here.So the above code means that if within combobox1 you go down a row and press it you should be able to get a yesNo msgbox and if you press yes then Userform1 should appear. 上面的代码不起作用,但是由于我缺乏知识,我不知道该怎么办,这就是我在这里的原因。所以上面的代码意味着,如果在combobox1中,您可以按一下并按下它要获得yesNo msgbox,如果按Yes,则应该出现Userform1。

I know its doable because ive managed to make userform 1 appear but my problem is VByesNO. 我知道它是可行的,因为ive设法使userform 1出现了,但是我的问题是VByesNO。

Ok so the solution - as PEH said - was to connect the condition via And and to remove msg . 好的,因此解决方案(如PEH所述)是通过And连接条件And删除msg

If ComboBox1.ListIndex = 1 And Msgbox("Do you want to create a new company?", vbYesNo) = vbYes Then UserForm1.Show

instead of 代替

If Combobox1.Listindex =1 Msgbox msg("Do you want to create a new company?", vbYesNo) = vbYes Then UserForm1.Show

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM