Populating a ComboBox in a UserForm

I

I Maycotte

Hi everyone,

My main question is: how do I populate a combobox located in a userfor
with items from an array? I thought I would use .AddItem like I would
listbox, but it simply will not populate. Here is the code:


Code
-------------------
Private Sub ComboBox1_Change()
Dim MyArray As Variant
Dim Ctr As Integer

MyArray = Array("Item1", "Item2", "Item3", "Item4", "Item5")

For Ctr = LBound(MyArray) To UBound(MyArray)
SampleReport.ComboBox1.AddItem MyArray(Ctr)
Next Ctr
End Su
-------------------


Any advice? Thanks in advance.

-- Isaa
 
K

kev_06

Currently, you're trying to add items to a combobox under a change
event. The combobox will populate if you enter a value in or somehow
change the text/value shown in a combobox. I think you're looking for
something that will load the combobox on start, in which case put the
code under a userform_initialize event. Something like this:

Private Sub UserForm_Initialize()
Dim MyArray As Variant
Dim Ctr As Integer

MyArray = Array("Item1", "Item2", "Item3", "Item4", "Item5")

For Ctr = LBound(MyArray) To UBound(MyArray)
ComboBox1.AddItem MyArray(Ctr)
Next Ctr
End Sub
 

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

Top