How to Create Multiple Dependent Drop Down Lists with Excel VBA – 3 Methods

 

What Are Dependent Drop Down Lists in Excel?

Using a dependent drop-down list, you can change the value of a drop-down list and it will show the values in another drop-down list.


The following dataset contains people’s names to whom different projects will be assigned, using a drop-down list. 


Method 1- Entering Multiple Data in a Cell from a Dependent Drop Down List with Excel VBA

There are two lists: Project Name and Project Members. One or multiple members will be assigned to each project, using a drop-down list.

Entering Multiple Data in a Cell from a Dependent Drop-Down List with Excel VBA

Steps:

  • Go to the Developer tab and open Visual Basic (Shortcut Alt + F11)

  • Go to the VBAProject menu.
  • Enter the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Declare variable
 Dim xOld_Val As String
 Dim xNew_Val As String
 'Enable event
 Application.EnableEvents = True
 On Error GoTo Exitsub
 'Target the range
 If Not Intersect(Target, Range("C5:C12")) Is Nothing Then
   If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
      GoTo Exitsub
   Else: If Target.Value = "" Then GoTo Exitsub Else
   'Enable event
      Application.EnableEvents = False
      'Specify target value into variable
    xNew_Val = Target.Value
      Application.Undo
      xOld_Val = Target.Value
      'Apply If else statement
         If xOld_Val = "" Then
            Target.Value = xNew_Val
         Else
            If InStr(1, xOld_Val, xNew_Val) = 0 Then
               Target.Value = xOld_Val & ", " & xNew_Val
         Else:
            Target.Value = xOld_Val
         End If
      End If
   End If
 End If
 Application.EnableEvents = True
Exitsub:
 Application.EnableEvents = True
End Sub 
			

VBA Breakdown
  • defines the subroutine:
Private Sub Worksheet_Change(ByVal Target As Range)
  • declares the variables: 
 Dim xOld_Val As String
Dim xNew_Val As String
  • specifies the targeted cell range validation from the worksheet:
If Not Intersect(Target, Range("C5:C12")) Is Nothing Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
  • specifies the target value into a variable:
xNew_Val = Target.Value
Application.Undo
xOld_Val = Target.Value
  • applies the If Else statement:
If xOld_Val = "" Then
Target.Value = xNew_Val
Else
If InStr(1, xOld_Val, xNew_Val) = 0 Then
Target.Value = xOld_Val & ", " & xNew_Val
Else:
Target.Value = xOld_Val
End Sub
  • Select multiple names in the Project Member column.

Read More: How to Use Named Range for Data Validation List with VBA in Excel


Method 2 – Creating a Multiple Dependent Drop Down List in a UserForm with Excel VBA

Steps:

  • Go to the Insert tab.
  • Click UserForm.
  • The Userform is created.
  • Add combo boxes and command buttons.
  • Here, position 3 was chosen to add a combo box on the left and the second combo box on the right side.
  • Add the command button by clicking position 4.

Creating a Multiple Dependent Drop-Down List in UserForm with Excel VBA

  • Right-click the UserForm to view the code.
  • Enter the following VBA code and press F5 to run it.
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "Animals_Name"
.AddItem "Sports_Type"
.AddItem "Food_Item"
End With
End Sub
Private Sub ComboBox1_Change()
Dim Xind As Integer
Xind = ComboBox1.ListIndex
ComboBox2.Clear
Select Case index
Case Is = 0
With ComboBox2
.AddItem "Tiger"
.AddItem "Lion"
.AddItem "Kangaroo"
End With
Case Is = 1
With ComboBox2
.AddItem "Football"
.AddItem "Cricket"
.AddItem "Badminton"
End With
Case Is = 2
With ComboBox2
.AddItem "Milk"
.AddItem "Chicken"
.AddItem "Mutton"
End With
End Select
End Sub

VBA BREAKDOWN

  • defines the subroutine:
Private Sub UserForm_Initialize()
  • adds three items using a combo box:
With ComboBox1
.AddItem "Animals_Name"
AddItem "Sports_Type"
AddItem "Food_Item"
End With
  • define a subroutine:
Private Sub ComboBox1_Change()
  • selects three cases for the three items.
  • In the first case, inputs the items in the animals category.
 Case Is = 0
        With ComboBox2
            .AddItem "Tiger"
            .AddItem "Lion"
            .AddItem "Kangaroo"
        End With
  • In the second case, inputs the items in the sports category.
    Case Is = 1
        With ComboBox2
            .AddItem "Football"
            .AddItem "Cricket"
            .AddItem "Badminton"
        End With
  • In the third case, inputs the items in the food category.
    Case Is = 2
        With ComboBox2
            .AddItem "Milk"
            .AddItem "Chicken"
            .AddItem "Mutton"
        End With
  • ends the macro:
End sub
  • You will see the UserForm.
  • Select Animals_Name from the first combo box.

  • You will see the list of animals from the second combo box.

Read More: Unique Values in a Drop Down List with VBA in Excel


Method 3 –  Clearing Mismatched Values from a Multiple Dependent Drop Down List with Excel VBA

 

Clearing Mismatched Values from a Multiple Dependent Drop-Down List with Excel VBA

Steps:

  • Open the VBA by following the steps described in method 1 and enter the following code.
Private Sub Worksheet_Change(ByVal Target As Range)
 On Error Resume Next
'Define target column as 3
If Target.Column = 3 Then
'Applying If condition for target validation type as 3
  If Target.Validation.Type = 3 Then
  'Disable event by declaring the statement as False
   Application.EnableEvents = False
  'Move down 1 row and to the right 0 column
   Target.Offset(0, 1).ClearContents
  End If
End If
exitHandler:
'Enable event by declaring the statement as True
  Application.EnableEvents = True
  Exit Sub
End Sub 
			

Clearing Mismatched Values from a Multiple Dependent Drop-Down List with Excel VBA

VBA Breakdown

  • Applies the If condition by defining the target column as 3.
If Target.Column = 3 Then
  • Applies the If the condition for target validation type as 3.
  If Target.Validation.Type = 3 Then
  • Disables the event by declaring the statement as False.
Application.EnableEvents = False
  • Moves down 1 row and to the right 0 column.
 Target.Offset(0, 1).ClearContents
  • Enables the event by declaring the statement as True.
  Application.EnableEvents = True

 

  • Select any items in the Food column and try to select a different category in Category:

  • Corn is not visible because it is not on the list of vegetables.

Clearing Mismatched Values from a Multiple Dependent Drop-Down List with Excel VBA


Things to Remember

Common Errors When they show
Unable to delete List In the Data Validation, if  Allow is not equal to List and the Source is not correctly selected, the drop-down list can’t be deleted. Use a VBA code to delete the list.
Update value problem In dependent drop-down lists, if there is any mismatched value, it will not update automatically. Use a formula or a VBA code (method 3) to update the value automatically.

Download Practice Workbook

Download the following Excel workbook.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Md. Abdullah Al Murad
Md. Abdullah Al Murad

Md. Abdullah Al Murad is a computer engineer who loves exploring Excel and VBA programming. Programming, to him, is a time-saving tool for efficiently managing data, files, and internet tasks. He is proficient in C, C++, Python, JavaScript, HTML, SQL, MySQL, PostgreSQL, Microsoft Office, and VBA and goes beyond the basics. He holds a B.Sc. in Computer Science and Engineering from American International University-Bangladesh. He has transitioned into the role of a content developer. Beyond his work, find... Read Full Bio

5 Comments
  1. “2. Create Multiple Dependent Drop-Down List in Excel VBA” is not working for me. When i run macro it ask which macro to run i.e. veg, fruit or dairy. Dropdown do not create. If possible send sample file of this.

    • Hi AVINASH. Check your email for a sample file. I have used the INDIRECT function with the Data Validation command to make it.
      Regards.

  2. Hello,

    In you second point, is it possible if I choose Vegetable_List, the right column automatically select third vegetable from the list, in this example, “Lettuce”?

  3. I have followed the steps # 1, 2 & 3 of “2. Create Multiple Dependent Drop-Down List in Excel VBA” but I only got the dropdown list for the “Category” column and not for the second column “Food”. What would be the issue?

    Thanks and regards,
    [email protected]

  4. you have three columns of values in Columns E, F & G for ‘Vegetable’, ‘Fruits’ & ‘Dairy_Product’, but nowhere in the code do you tell the code to refer to these values.
    How does the code know that it should refer to the values in these tables?

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo