0
votes

I have two combobox.The first combobox has three items as:{One, Tow, Three} now I would like to load the second combobox based on what user select in combobox1. For example if user select One in the from combobox1 then namebox1 will populate to combox2 and if if user select Two in the from combobox2 then namebox2 will populate to combox2 and so on.. Can you please let me know how I can do this in VBA?

Thanks

Here is the Update Code:

    Private Sub ComboBox1_Change()
     Me.ComboBox2.Clear
     Select Case Me.ComboBox1.Value
       Case "One"
        With Me.ComboBox2
            .RowSource = "nameBox1"
        End With
       Case "Two"
        With Me.ComboBox2
            .RowSource = "nameBox1"
        End With
       Case "Three"
        With Me.ComboBox2
             .RowSource = "nameBox1"
        End With
     End Select
    End Sub

Please be advise that I didn't use .addItem to populate the ComboBox1. It hasbeen populate by same method .RowSource which is usinf excel collection selection box

1

1 Answers

1
votes

The easiest way to do this would be using a Select Case statement. Suppose you had

Private Sub ComboBox1_Change()

Me.ComboBox2.Clear

Select Case Me.ComboBox1.Value
    Case "One"
        ' If your data was information you wanted to put in yourself:
        With Me.ComboBox2
            .AddItem "Adam"
            .AddItem "Allen"
            .AddItem "Andy"
        End With
    Case "Two"
        ' If your data existed in a worksheet range
        ComboBox2.RowSource = MyRange.Address            
        End With
    End Select

End Sub

I hope this explains enough to get you going - Otherwise, I'm a bit confused - Can you explain what you mean by NameBox in your question