Say you set up your data validation lists on Sheet2, and you set up your first data validation on Sheet1!A2
. And you wanted to change the validation of Sheet1!B2
based on what was selected in Sheet1!A2
. You would place this code in the worksheet module for Sheet1:
Code
Sub worksheet_change(ByVal Target As Range)
If Not Intersect(Target, Range("A2")) Is Nothing Then
If Target.Value = "one" Then
With Range("B2").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet2!B1:B2"
End With
End If
If Target.Value = "two" Then
With Range("B2").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet2!B3:B4"
End With
End If
If Target.Value = "three" Then
With Range("B2").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet2!B5:B6"
End With
End If
End If
End Sub
Then you would set up additional if
statements for each additional list that you want to add validation to.
Explanation
Basically this is saying if there's a change on the worksheet, check to see if it's at A2
and it contains a value. This value will only be within your data validation sheet. So if these things are true then:
Check for each possible value and then set the data validation of B2
to the possibilities defined in your data validation list as referenced.
Expand the possibilities to fit your data and add additional tiers for additional lists.