Marius
Marius

Reputation: 7

Multiple Dependent Drop-Down Lists in Excel VBA

I want to create multiple drop down lists with an additional twist.

The two sheets of the workbook.

enter image description here

enter image description here

I'm trying to create a dropdown list for cell B6 in the "FORM" sheet which is pulled from cells A2:A4 in the "LISTS" sheet. Based on what is chosen in cell B6, a dropdown list (pulled from the "LISTS" sheet) for cell D6 in the "FORM" sheet will be created. The caveat here is that if "PART" is chosen from the dropdown list in cell B6 of the "FORM" sheet, instead of a generating a dropdown list in D6, I'd like the cell to show "N/A" instead.

Sub PRODUCT_LIST()

Dim FORM As Worksheet
Dim LISTS As Worksheet
Dim PRODUCT As Range
Dim PRODUCT_LIST As Range
Dim MODEL As Range
Dim BIKE_LIST As Range
Dim CHAIR_LIST As Range

Set FORM = ThisWorkbook.Worksheets("FORM")
Set LISTS = ThisWorkbook.Worksheets("LISTS")
Set PRODUCT = FORM.Range("B6")
Set MODEL = FORM.Range("D6")
Set PRODUCT_LIST = LISTS.Range("A2:A4")
Set BIKE_LIST = LISTS.Range("B2:B8")
Set CHAIR_LIST = LISTS.Range("C2:C3")

With PRODUCT.Validation
    .Delete 'delete previous validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
        Formula1:="='" & LISTS.Name & "'!" & PRODUCT_LIST.Address
End With

If PRODUCT.Value = "BIKE" Then
    With MODEL.Validation
        .Delete 'delete previous validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Formula1:="='" & LISTS.Name & "'!" & CHAIR_LIST.Address
    End With
ElseIf PRODUCT.Value = "CHAIR" Then
    With MODEL.Validation
        .Delete 'delete previous validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Formula1:="='" & LISTS.Name & "'!" & BIKE_LIST.Address
    End With
ElseIf PRODUCT.Value = "PART" Then
    MODEL.Value = "N/A"
Else
End If
End Sub

The dropdown list for B6 works but the dropdown list for D6 seems to be ignoring the conditions that I've placed. Regardless of what I choose in the dropdown list from B6, the dropdown list for D6 is always pulling from CHAIR_LIST.

Upvotes: 0

Views: 2410

Answers (1)

FaneDuru
FaneDuru

Reputation: 42236

You did not answer the clarification questions...

Please, try the next approach:

  1. Firstly you need to create the validation for the first cell. Only once, or if needed to modify the list content. Please, copy the next code in a standard module:
Sub PRODUCT_LIST()
  Dim FORM As Worksheet, LISTS As Worksheet
  
  Set FORM = ThisWorkbook.Worksheets ("FORM")
  Set LISTS = ThisWorkbook.Worksheets("LISTS")
  
  
  With FORM.Range("B6").Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                 Formula1:="='" & LISTS.name & "'!" & "A2:A4"
  End With
End Sub
  1. Please, copy the next code in the FORM sheet code module. Right click on the sheet name, then choose View Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) = "B6" Then
        Dim MODEL As Range, LISTS As Worksheet, BIKE_LIST As Range, CHAIR_LIST
        
        Set MODEL = Me.Range("D6")
        Set LISTS = ThisWorkbook.Worksheets("LISTS")
        Set BIKE_LIST = LISTS.Range("B2:B8")
        Set CHAIR_LIST = LISTS.Range("C2:C3")
        
        Application.EnableEvents = False
         If Target.value = "BIKE" Then
            With MODEL.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Formula1:="='" & LISTS.name & "'!" & BIKE_LIST.Address
            End With
            MODEL.value = ""
         ElseIf Target.value = "CHAIR" Then
            With MODEL.Validation
                .Delete 'delete previous validation
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Formula1:="='" & LISTS.name & "'!" & CHAIR_LIST.Address
            End With
            MODEL.value = ""
         ElseIf Target.value = "PART" Then
            MODEL.value = "N/A"
         Else
     End If
     Application.EnableEvents = True
    End If
End Sub

If after changing the MODEL validation list you need to place a value from this list (let us say, the first one), it can be easily done...

Upvotes: 0

Related Questions