Cascading ComboBoxes

aaronlive

New Member
Joined
Dec 23, 2009
Messages
8
I apologize if this is too elementary for some on the forum. I'm just trying to create a user form with two cascading comboBoxes (i.e. the list in box2 is dependent on the selection in box1). I've looked all over and have tried various code suggested on different forums, but I just can't get it to work. I think there are two parts to the question. First, what is the appropriate code? Second, where do I put the code (i.e. is it just a sub in the sheet or is it part of the combobox or somewhere else)?

Thanks in advance for any help.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
something like
Code:
Private Sub ComboBox1_Change()
 
    If ComboBox1.Value = "" Then
        ComboBox2.Clear
    ElseIf ComboBox1.Value = "option1 on combobox1" Then
        ComboBox2.Clear
        ComboBox2.SetFocus
        With ComboBox2
                .AddItem "first item"
                .AddItem "second item"
        End With
    ElseIf ComboBox1.Value = "option2 on combobox1" Then
        ComboBox2.Clear
        ComboBox2.SetFocus
        With ComboBox2
                .AddItem "third item"
                .AddItem "fourth item"
        End With
    End If
 
End Sub
You'll need to edit the items, but that's how I would go about it.

Edit: The code will need to go into the code on the Form itself. You can get to this by clicking the "View Code" icon in the VBA Project window or pressing F7 from the Form in the editor.
 
Last edited:
Upvote 0
Thanks for the reply. Do you have a suggestion with using a Range as the source for box2? I've tried things like:

ComboBox2.RowSource = Range("A1:A5").Address

or

ComboBox2.RowSource = Range(Cells(Row_1, Col_2), Cells(Row_5, Col_2)).Address
Both of which didn't work.
 
Upvote 0
I wonder if you can help me with a related question. I'm trying to load the comboboxes with data from a different worksheet. However, no matter what code I try it only loads from the active sheet. Here's an example of one of the subs that I tried. Let me know if you have any ideas how to fix it. Appreciate the help!

Private Sub ComboBox1_Change()

If ComboBox1.Value = "" Then
ComboBox2.Clear
ElseIf ComboBox1.Value = "Income" Then

ComboBox2.RowSource = Worksheets("Sheet3").Range("F1", "F5").Address

ElseIf ComboBox1.Value = "Costs" Then
ComboBox2.RowSource = Worksheets("Sheet3").Range("G1", "G5").Address
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top