Adding the same content to multiple combo boxes

K

keaven

Okay.. here's the deal, i have a user form in excel that
gathers information. In one section there is a series of
six combo boxes. What i have been trying to do is have
code in the UserForm_Initialize() sub that adds items to
the combo boxes. All six combo boxes need the same
information added to them.

what i would like to do is something like:
for each X in (1,2,3,4,5,6)
with Me.ComboBoxX
.AddItem "a"
.AddItem "b"
end with
next

trying to get it to go through all 6 boxes.. but this is
not working and i cant figure out how to get it to work
without reapeating the .AddItem list for each and every
box. is there a faster way to do it?

-keaven
 
C

Chip Pearson

Keaven,

Load up combobox 1, and then set the List property of each of the
other comboboxes to the List property of combobox 1.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
T

Tom Ogilvy

with Me.ComboBox1
.AddItem "a"
.AddItem "b"
end with

for i = 2 to 6
me.Controls("Combobox" & i).List = Combobox1.List
Next
 
G

Guest

Chip, Tom.. thanks for your help, it worked great.
the second part was to have an item be removed from all
the other combo boxes once it was selected from another
one. this was complicated byt he fact that there was no
way to know in what order the comboboxes would be used.

This is how i ended up doing it... basically storing the
list index of the selection into a variable, then making
sure that the other comboboxes didnt have count of zero
(since i clear the combobox list after a choice is made),
then removing the corresponding list index. This is for
the first box, the other five are exactly the same, with
the exception of combobox referances of corse:

Private Sub StatBox1_Click()
' Lockes the stat choice in, and removes it
' from the other comboboxes
Dim strStat As String
Dim X As Integer
X = Me.StatBox1.ListIndex
'MsgBox X
strStat = Me.StatBox1.Value
Me.StatBox1.Clear
Me.StatBox1.Value = strStat
Me.StatBox1.Locked = True

Call FindStat(Me.StatBox1.Value, Me.stat1.Value)

If (Me.StatBox2.ListCount > 0) Then
Me.StatBox2.RemoveItem (X)
End If
If (Me.StatBox3.ListCount > 0) Then
Me.StatBox3.RemoveItem (X)
End If
If (Me.StatBox4.ListCount > 0) Then
Me.StatBox4.RemoveItem (X)
End If
If (Me.StatBox5.ListCount > 0) Then
Me.StatBox5.RemoveItem (X)
End If
If (Me.StatBox6.ListCount > 0) Then
Me.StatBox6.RemoveItem (X)
End If
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