简体   繁体   中英

Excel formula that will let me show a data range in a cell based on the value in another cell

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?

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:

在此处输入图片说明

There's a solution for this using formulas alone, without VBA code.

Assuming your data looks like this:

动态验证清单

  • First column selection list is Catgories, in A2:A4.
  • Second column selection list will be chosen as one of C2:C6 | D2:D6 | E2:E6, by the selection of a category.
  • In G2:G3 you can select a category.
  • H2:H3 will show the list according to the category selected in G respectively.

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 Formula:

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.

Remarks:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM