ComboBox Name array

  • Thread starter Thread starter Stacy
  • Start date Start date
S

Stacy

I have a number of ComboBoxes on a Userform that all do the same
thing. I would like to be able to do all the init stuff in a For loop.
How can I refer to the ComboBoxes without specificing each name. The
following would be great but, of course it doesn't work. Any ideas?

ComboBoxArray = Array("ComboBox1", "ComboBox2", "ComboBox3",
"ComboBox4", "ComboBox5")

For i = 1 To NumComboBoxes
With HeaderUserForm.ComboBoxArray(i)
.Clear ' clear the listbox content
.ListIndex = 0 ' select the first item
' Do the other stuff
End With

Next i
 
Hello

Dim cCol As Collection
Dim ctrCombo As ComboBox
Dim n As Long
Set cCol = New Collection
For n = 1 To 5
cCol.Add Me.Controls("ComboBox" & n)
Next n
For Each ctrCombo In cCol
With ctrCombo
'Some nonsense for testing
For n = 1 To 100
.AddItem "Number" & n
Next n
.ListIndex = (100 * Rnd + 1)
End With
Next ctrCombo

Heiko
:-)
 
Back
Top