Consulting

Results 1 to 14 of 14

Thread: Looping a combobox

  1. #1
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location

    Looping a combobox

    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

  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Assuming your comboboxes are called Combobox1, Combobox2 and Combobox3, you can use:
    [VBA] for i=1 to 3
    Me.Controls("Combobox" & i) = "whatever"
    Next i
    [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    VBAX Regular
    Joined
    May 2007
    Posts
    18
    Location
    If your comboboxes are random names then

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

  4. #4
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Quote Originally Posted by rory
    Assuming your comboboxes are called Combobox1, Combobox2 and Combobox3, you can use:
    [vba] for i=1 to 3
    Me.Controls("Combobox" & i) = "whatever"
    Next i
    [/vba]
    I have tried that, and it worked for only one item. thanks
    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.

  5. #5
    VBAX Regular
    Joined
    May 2007
    Posts
    18
    Location
    ....

    Quote Originally Posted by Reafidy
    If your comboboxes are random names then

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

  6. #6
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Quote Originally Posted by Reafidy
    ....
    I have also tried the code from Reafidy and it gave me the same result.

  7. #7
    VBAX Regular
    Joined
    May 2007
    Posts
    18
    Location
    Sorry, I dont understand, can you better explain what the problem is please?

  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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

  9. #9
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Quote Originally Posted by Reafidy
    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.

  10. #10
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Quote Originally Posted by mikerickson
    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
    Thanks!!!!
    Is it possible to do that without the first combobox already in place?

  11. #11
    VBAX Regular
    Joined
    May 2007
    Posts
    18
    Location
    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:

    [vba]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[/vba]

    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

  12. #12
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Quote Originally Posted by Reafidy
    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:

    [vba]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[/vba]

    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. Thanks a lot.
    How would you modify rory's code to do the same thing?

  13. #13
    VBAX Regular
    Joined
    May 2007
    Posts
    18
    Location
    Rorys code (modified):

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

  14. #14
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location

    Thumbs up

    Awesome Guys, thank you so much for your help, and for your patience.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •