Excel: How to create dynamic data validation list based on data table

后端 未结 4 900
无人及你
无人及你 2020-12-10 18:07

Imagine I\'m writing a menu-planner in Excel for my kids (easier to describe than my actual problem) ...

I have a list of available foods: apples, bananas, carrots,

4条回答
  •  攒了一身酷
    2020-12-10 18:59

    I assumed that your data table is in range A1:E7.

    Step 1. Create a list of choices for each kid

    For each kid create a list with all their preferences listed (at the end of the list I added "-" as placeholders). Enter this formula in G2 and drag to range G2:J7:

    =IF(G1="-";"-";IF(ISNA(OFFSET($A$1;IFERROR(MATCH(G1;$A$2:$A$7;0);0)+
    MATCH("x";OFFSET(B$2;IFERROR(MATCH(G1;$A$2:$A$7;0);0);0;7;1);0);0;1;1));
    "-";OFFSET($A$1;IFERROR(MATCH(G1;$A$2:$A$7;0);0)+MATCH("x";OFFSET(B$2;
    IFERROR(MATCH(G1;$A$2:$A$7;0);0);0;7;1);0);0;1;1)))
    

    Also put kids names above data columns (G1:J1).

    Step 2. Create conditional data validation

    Given that your first data validation list (name) is in cell L2 and you've followed step 1, use this formula for data validation for food:

    =OFFSET(F$2;0;MATCH(L2;$G$1:$J$1;0);6-COUNTIF(OFFSET(F$2:F$7;0;
    MATCH(L2;$G$1:$J$1;0));"-"))
    

    This formula both excludes all empty choices ("-") in the list and gives the right list based on kid's name.


    UPDATE. Alternative solution with INDEX/MATCH

    OFFSET is a volatile formula (i.e. Excel recalculates it whenever there is any change in your workbook) so you might want to do this with INDEX instead. Here is the formula for my step 1 above:

    =IF(G1="-";"-";IFERROR(INDEX($A$2:$A$7;IFERROR(MATCH(G1;$A$2:$A$7;0);0)+
    MATCH("x";INDEX(B$2:B$7;IFERROR(MATCH(G1;$A$2:$A$7;0)+1;1);1):B$7;0);1);"-"))
    

    As for the step two, it seems that formula for data validation gets recalculated only when you select the cell so OFFSET doesn't have volatility in data validation lists. As INDEX cannot return a range and Excel doesn't allow INDEX(..):INDEX(..) ranges for data validation, OFFSET is better for data validation lists.

提交回复
热议问题