OzGrid

Matching ComboBox Controls

< Back to Search results

 Category: [Excel]  Demo Available 

Matching ComboBox Controls

 

Excel VBA: Dependent Lists for Combobox Controls

Got any Excel/VBA Questions? Free Excel Help 

 

Matching ComboBoxes

We can use Excel VBA to have one ComboBox Control dependent on the item chosen in another. It makes use of dynamic named ranges and the lists laid out on a Worksheet (can be hidden), with appropriate headings and each list named the same as the items in the first ComboBox. Then we can use code like shown below:

Private Sub ComboBox1_Change()

Dim strRange As String

    If ComboBox1.ListIndex > -1 Then

       strRange = ComboBox1

       Label2.Caption = strRange

       strRange = Replace(strRange, " ", "_")

            With ComboBox2

                .RowSource = vbNullString

                .RowSource = strRange

                .ListIndex = 0

            End With

    Else

       Label2.Caption = "Associated Items"

    End If

End Sub

Note the use of Replace to replace any space characters with the underscore. This is needed in some cases as range names cannot contain spaces

Download the Workbook Example

 

See also:

Debug Excel VBA Macro Code
Excel VBA - Excel Macros
Delete Module After Running VBA Code
Deleting Excel Named Ranges
Delete Excel Sheet Event Code Macro
Delete Excel Workbook Event Code

 

Free Training Course: Lesson 1 - Excel Fundamentals

 

See also: Index to Excel VBA Code; Index to Excel Freebies; Lesson 1 - Excel Fundamentals; Index to how to… providing a range of solutions

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


Gallery



stars (0 Reviews)