PDA

View Full Version : Looping a combobox



fadib
11-15-2007, 02:16 PM
Hi guys,
is it possible, if I have three combobox, and I want to fill them up with the same values, to write a loop to do so?

example:
for i=1 to 3
i=i+1
combobox (i) = .....
.
.
.
loop
:think:

rory
11-15-2007, 03:23 PM
Assuming your comboboxes are called Combobox1, Combobox2 and Combobox3, you can use:
for i=1 to 3
Me.Controls("Combobox" & i) = "whatever"
Next i

Reafidy
11-15-2007, 03:28 PM
If your comboboxes are random names then


Dim ctlLoop As msforms.Control
For Each ctlLoop In Me.Controls
If TypeOf ctlLoop Is msforms.ComboBox Then ctlLoop.Value = "YrValue"
Next ctlLoop

fadib
11-15-2007, 03:44 PM
Assuming your comboboxes are called Combobox1, Combobox2 and Combobox3, you can use:
for i=1 to 3
Me.Controls("Combobox" & i) = "whatever"
Next i


I have tried that, and it worked for only one item. thanks :thumb
how can I do the same thing but with multiple item? (I want to be able to add more items)
I tried to add the lines inside the loop, but it is only recording the last item only.

Reafidy
11-15-2007, 04:00 PM
....


If your comboboxes are random names then


Dim ctlLoop As msforms.Control
For Each ctlLoop In Me.Controls
If TypeOf ctlLoop Is msforms.ComboBox Then ctlLoop.Value = "YrValue"
Next ctlLoop

fadib
11-15-2007, 04:18 PM
....

I have also tried the code from Reafidy and it gave me the same result. :(

Reafidy
11-15-2007, 04:24 PM
Sorry, I dont understand, can you better explain what the problem is please?

mikerickson
11-15-2007, 04:32 PM
If your data is in an array, this should work.



UserForm1.ComboBox1.List = dataArray
UserForm1.ComboBox2.List = dataArray
UserForm1.ComboBox3.List = dataArray



If the list for the first combo box is already in place, try this,


With UserForm1
.ComboBox2.Clear
.ComboBox3.Clear
For i = 0 To .ComboBox1.ListCount - 1
.ComboBox2.AddItem .ComboBox1.List(i)
.ComboBox3.AddItem .ComboBox1.List(i)
Next i
End With

fadib
11-15-2007, 04:32 PM
Sorry, I dont understand, can you better explain what the problem is please?
Sorry if I am not able to deliver the message the right way.
let's see,
I have a form, in which I created three combobox: combobox1, combobox2, combobox3.
I know how to write the code to additem to a single combobox.
what I am trying to do is if I have 100 comboxbox, I don't want to write the same code for each combobox.

I am trying to loop that, so I can save time.
example:
combobox1 will have in it: item1, item2, item3, item4
combobox2 the same
combobox3 as well.

I hope my description is efficient.

N.B: I am new to VBA.

fadib
11-15-2007, 04:37 PM
If your data is in an array, this should work.



UserForm1.ComboBox1.List = dataArray
UserForm1.ComboBox2.List = dataArray
UserForm1.ComboBox3.List = dataArray



If the list for the first combo box is already in place, try this,


With UserForm1
.ComboBox2.Clear
.ComboBox3.Clear
For i = 0 To .ComboBox1.ListCount - 1
.ComboBox2.AddItem .ComboBox1.List(i)
.ComboBox3.AddItem .ComboBox1.List(i)
Next i
End With

It worked :thumb :)
Thanks!!!!
Is it possible to do that without the first combobox already in place?

Reafidy
11-15-2007, 04:38 PM
Im pretty sure the code rory and I have given you will do that.

For example if you have three comboboxes on a userform then run this:

Private Sub CommandButton1_Click()
Dim ctlLoop As msforms.Control
For Each ctlLoop In Me.Controls
If TypeOf ctlLoop Is msforms.ComboBox Then
ctlLoop.List = Array("Item1", "Item2", "Item3")
End If
Next ctlLoop
End Sub

All comboboxs will have them 3 items availble from the dropdown.

You can add as many comboboxes as you like and the code will fill them all with the items.

You can add as many items to the comboboxes as you want by changing the array

fadib
11-15-2007, 04:44 PM
Im pretty sure the code rory and I have given you will do that.

For example if you have three comboboxes on a userform then run this:

Private Sub CommandButton1_Click()
Dim ctlLoop As msforms.Control
For Each ctlLoop In Me.Controls
If TypeOf ctlLoop Is msforms.ComboBox Then
ctlLoop.List = Array("Item1", "Item2", "Item3")
End If
Next ctlLoop
End Sub

All comboboxs will have them 3 items availble from the dropdown.

You can add as many comboboxes as you like and the code will fill them all with the items.

You can add as many items to the comboboxes as you want by changing the array

YEP it worked, Awesome.:beerchug: Thanks a lot.
How would you modify rory's code to do the same thing?

Reafidy
11-15-2007, 04:56 PM
Rorys code (modified):

Dim iLoop As Integer
For iLoop = 1 To 3
Me.Controls("Combobox" & iLoop).List = Array("Item1", "Item2", "Item3")
Next iLoop

fadib
11-15-2007, 05:22 PM
Awesome Guys, thank you so much for your help, and for your patience. :clap: