My ComboBox doesn't display the values I've added in VBA

女生的网名这么多〃 提交于 2020-01-11 07:04:54

问题


I'm trying to add options to a combo box in a userform. When I run the code, Excel doesn't give any errors, however when the userform shows up it doesn't display the entities I have added to the combobox previously. That is, when I click on the combobox, it doesn't show any options, only one blank row, as if no items were added to it.

Here is the code I'm using:

Private Sub UserForm_Initialize()
    ComboBox1.AddItem "xxx"
    ComboBox1.AddItem "yyy"
    ComboBox1.AddItem "zzz"
End Sub

I am using the following code to call the user form within a macro:

UserForm.Show

回答1:


The code given in the question works perfectly well. In my case the code didn't work because I manually entered this part of the code into VBA:

Private Sub UserForm_Initialize()

If you make Excel create this module for you instead of writing it on your own, your code should work perfectly. Excel did not have "Initialize" as a default form so I tried "Activate" and it worked.

To create this module you have to do the following steps:

  1. Right click on user form
  2. Click on view code
  3. On top you will see two categories you can pick, you should pick "Userform" and "Activate", after completing this step excel must add a new module to your code.
  4. In this module you may code everything you want about the content of the combobox.

You should also be careful with the spelling of your combobox, if you spell it incorrectly, you may be unable to see the contents of the combobox.




回答2:


Ensure that the code segment you have posted is in the userform.

Right click on the user form in the VBA view and choose "View Code". Is this where the code is?

Are you sure that the User Form is called 'UserForm' and not 'UserForm1'? 'UserForm1' is the default, similar to 'ComboBox1'.

The below works for me.

'in the UserForm1 code
Private Sub UserForm_Initialize()
ComboBox1.AddItem "xxx"
ComboBox1.AddItem "yyy"
ComboBox1.AddItem "zzz"
End Sub

The below will display the form.

UserForm1.Show

Is this the only form in the workbook? Create a new one and see if it does the same thing.



来源:https://stackoverflow.com/questions/24219796/my-combobox-doesnt-display-the-values-ive-added-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!