Reputation: 3
I am trying to create a form where someone can choose a value from a drop down list and it will create another dropdown list in the next cell based on the first cells value. The next cells dropdown list would need to reference a cell range on a separate worksheet and only show row 4-30 as a dropdown.
Is this possible? I tried using data validation with an indirect formula but I cannot define the cell ranges as the codes start with numbers.
Any suggestions would be helpful?
Upvotes: 0
Views: 414
Reputation: 2289
There's a solution for this using formulas alone, without VBA code.
Assuming your data looks like this:
The validation list source for H2:H3 will be defined with this formula:
=CHOOSE(MATCH(G2,$C$1:$E$1,0),$C$2:$C$6,$D$2:$D$6,$E$2:$E$6)
Select H2:H3, go to Data Validation, choose List and put this formula in the Source. (The range reference will be updated for H3 automatically).
The MATCH
part returns 1,2 or 3 for the category names in C1:E1.
CHOOSE
returns the range C, D or E respectively, according to the order they are supplied in the function.
The ranges can reference cells in another sheet by simply stating the sheet name Sheet2!C2:C6
in the validation list formula. In earlier versions of Excel you need to name these external ranges for it to work in a validation list.
Upvotes: 1
Reputation: 96773
Here is a very simple example using VBA
Place this event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A1 As Range, B1 As Range
Set A1 = Range("A1")
Set B1 = Range("B1")
If Intersect(A1, Target) Is Nothing Then Exit Sub
Select Case A1.Value
Case "animals"
Call SetupDV(B1, "dog,cat,bird")
Case "days"
Call SetupDV(B1, "monday,tuesday,wednesday")
Case "months"
Call SetupDV(B1, "january,february,march")
End Select
End Sub
and place this macro in a standard module:
Sub MAIN()
Cells.Validation.Delete
Call SetupDV(Range("A1"), "animals,days,months")
End Sub
Sub SetupDV(MyCells As Range, st As String)
With MyCells.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:=st
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
Then run MAIN
Whenever you make a selection from the pull-down in cell A1, the pull-down in cell B1 will adjust accordingly:
Upvotes: 1