Search code examples
vbaexcelcomboboxuserform

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


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

Solution

  • 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.