ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   ComboBox Name array (https://www.excelbanter.com/excel-programming/272669-combobox-name-array.html)

Stacy

ComboBox Name array
 
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

Heiko

ComboBox Name array
 
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
:-)
(Stacy) wrote:

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




All times are GMT +1. The time now is 05:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com