EASY: Populate multiple como boxes same criteria

  • Hey guys, what I'm trying to do is easy but I'm unsure as how to do it as I've never learnt VBA properly and I skipped all my readings on how to use loops! So I usually take the long way to do things, which for a vba expert would laugh at! Consider the code below. I have 27 combo boxes and I want all the combo boxes to populate data from a range. But I have to manually add this code to every single combo box which is timely. Is there a shorter way that could achieve what I'm after? I'm sure there is


    ComboBox1.SetFocus


    Dim c As Range

    With Sheets("Sheet1")
    For Each c In .Range("A:A", .Range("A" & Rows.Count).End(xlUp))
    If c.Value <> "" Then ComboBox1.AddItem c.Value
    Next c
    End With



    Dim c1 As Range

    With Sheets("Sheet1")
    For Each c1 In .Range("A:A", .Range("A" & Rows.Count).End(xlUp))
    If c1.Value <> "" Then ComboBox2.AddItem c1.Value
    Next c1
    End With

    Dim c2 As Range

    With Sheets("Sheet1")
    For Each c2 In .Range("A:A", .Range("A" & Rows.Count).End(xlUp))
    If c2.Value <> "" Then ComboBox3.AddItem c2.Value
    Next c2
    End With



    Dim c3 As Range

    With Sheets("Sheet1")
    For Each c3 In .Range("A:A", .Range("A" & Rows.Count).End(xlUp))
    If c3.Value <> "" Then ComboBox4.AddItem c3.Value
    Next c3
    End With





    I've just used 4 combo boxes as an example of what I'm doing, but surely there is a quicker way so I don't have to do this for all 27!



    Please help :)

  • Re: EASY: Populate multiple como boxes same criteria


    BONUS QUESTION


    Similarly, I think I'm taking the long way to do what I'm trying to achieve below too.


    Dim found As Range
    Set found = Sheets("Sheet1").Columns("A").Find(what:=ComboBox1.Value, LookIn:=xlValues, lookat:=xlWhole)
    Dim found1 As Range
    Set found1 = Sheets("Sheet1").Columns("A").Find(what:=ComboBox2.Value, LookIn:=xlValues, lookat:=xlWhole)
    Dim found2 As Range
    Set found2 = Sheets("Sheet1").Columns("A").Find(what:=ComboBox3.Value, LookIn:=xlValues, lookat:=xlWhole)
    Dim found3 As Range
    Set found3 = Sheets("Sheet1").Columns("A").Find(what:=ComboBox4.Value, LookIn:=xlValues, lookat:=xlWhole)
    Dim found4 As Range
    Set found4 = Sheets("Sheet1").Columns("A").Find(what:=ComboBox5.Value, LookIn:=xlValues, lookat:=xlWhole)





    found.Select
    ActiveCell.Offset(rowOffset:=0, columnOffset:=1).Activate
    ActiveCell = ActiveCell.Value + 1



    found1.Select
    ActiveCell.Offset(rowOffset:=0, columnOffset:=1).Activate
    ActiveCell = ActiveCell.Value + 1



    found2.Select
    ActiveCell.Offset(rowOffset:=0, columnOffset:=1).Activate
    ActiveCell = ActiveCell.Value + 1



    found3.Select
    ActiveCell.Offset(rowOffset:=0, columnOffset:=1).Activate
    ActiveCell = ActiveCell.Value + 1



    found4.Select
    ActiveCell.Offset(rowOffset:=0, columnOffset:=1).Activate
    ActiveCell = ActiveCell.Value + 1






    Essentially, if a value in the combobox is in the range, it will put a 1 in the cell to the right of it. But I'm surely repeating myself here with all this copying and pasting and changing of values when I could use a loop I'm assuming?

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!