VBA Combo Box

G

Guest

I have a combo box that I want to populate with a list of names.

I do not want to reference names in the worksheet - using "RowSource" - I
want to write the names in the code.

I tried the following code:

Private Sub ComboBox1_Change()
With ComboBox1
..RowSource = ""
..AddItem "Male"
..AddItem = "Female"
End With
End Sub

This gives me an error. Does anyone know where the code should be added.
 
D

Dave Peterson

Is this on a userform?

If yes, then maybe it should be in the Userform_initialize procedure?

If it's on a worksheet, maybe in the auto_open or workbook_open procedures.
 
G

Guest

:

It is in a form

Private Sub UserForm_Initialize()
With ComboBox1
..RowSource = ""
..AddItem "Male"
..AddItem = "Female"
End With
End Sub

Gives an error. Is this what you meant by "Userform_Initialize" procedure
 
D

Dave Peterson

Yep.

And I didn't notice your typo in the original post:

.AddItem = "Female"
should be:
.AddItem "Female"

(no equal sign)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top