Sub topic 10
Build dependent drop-downs
A dependent drop-down is a drop-down whose options change depending on the value in another cell. If A2 holds "Fruit", the drop-down in B2 offers apples, pears and plums. If A2 holds "Dairy", the same drop-down offers milk and cheese.
This chapter assumes you can select ranges, write basic formulas, tell A2 from $A$2, and refer to another sheet with Lists!A2:A. It also assumes you have built a drop-down from a range, as covered in the earlier chapter on range-driven drop-downs. The approach below combines that drop-down with a FILTER formula.
How the pieces fit together
Sheets has no setting that says "make this drop-down depend on that cell". You build the dependency from formulas. A helper formula calculates the valid options for the current choice. The second drop-down reads its options from the cells the helper fills.
Whenever A2 changes, the helper recalculates, the helper range changes, and the B2 drop-down shows the new options.
Step 1: Lay out the source list
Create a sheet named Lists. Store one row per valid pair, with the category in column A and the item in column B. Keep every category name spelled identically each time it appears.
| A (Category) | B (Item) |
|---|---|
| Category | Item |
| Fruit | Apple |
| Fruit | Pear |
| Fruit | Plum |
| Vegetable | Carrot |
| Vegetable | Leek |
| Dairy | Milk |
| Dairy | Cheese |
Row 1 holds the headers and the data starts in row 2. This "long" layout is easier to maintain than one column per category. To add an item, you add a row, and no formulas need editing.
Step 2: Build the first drop-down
The first drop-down needs each category once. In Lists!D1 type Categories. In Lists!D2 enter:
=SORT(UNIQUE(FILTER(A2:A, A2:A<>"")))FILTER removes the blank rows below your data. UNIQUE collapses the repeated category names. SORT orders them. The result spills down column D.
Now create an Orders sheet with Category in A1 and Item in B1. Select Orders!A2, then choose Data > Data validation > Add rule. Set the criteria to Dropdown (from a range) and enter Lists!D2:D as the range. Under Advanced options, set "If the data is invalid" to Reject the input. Click Done.
The open-ended range D2:D means new categories appear automatically. Empty cells at the end of the range are not shown as options.
Step 3: Write the helper formula
On Orders, type Helper (do not edit) in E1. In E2 enter:
=IF(A2="", "", IFERROR(TRANSPOSE(FILTER(Lists!$B$2:$B, Lists!$A$2:$A=A2)), ""))Each part does one job:
FILTER(Lists!$B$2:$B, Lists!$A$2:$A=A2)returns the items whose category equals the choice in A2. This is the formula from the chapter's code examples.TRANSPOSEturns that vertical result into a horizontal one, so the options spill to the right along row 2 (E2, F2, G2 and so on).IFERRORreplaces the#N/AthatFILTERreturns when nothing matches.IF(A2="", "", ...)handles an empty A2. Without it, the conditionLists!$A$2:$A=A2would match every blank row in column A. Those blank rows would flow into the options.
The reason for the horizontal spill appears in the multiple-rows section below. Pick "Fruit" in A2 and check that E2:G2 now show Apple, Pear and Plum.
Step 4: Build the second drop-down
Select Orders!B2 and add a rule. Set the criteria to Dropdown (from a range) and enter Orders!E2:Z2 as the range. Make it wider than your longest category. Set invalid input to Reject the input and click Done.
Pick "Vegetable" in A2 and open B2. You should see Carrot and Leek. Switch A2 to "Dairy" and B2 offers Milk and Cheese.
Using the pattern on many rows
Real input areas usually have many rows, each needing its own pair of drop-downs. This is where the choice of a horizontal spill matters.
Copy the helper formula down column E, so E3 reads A3, E4 reads A4, and so on. Each row's helper spills to the right in its own row, and the spills never collide. If you had spilled downward, each formula would run into the one below it.
The harder part is the second drop-down. The range in a "Dropdown (from a range)" rule is a fixed reference. It is not shifted for each row the way a relative reference in a custom formula is. If you apply the rule Orders!E2:Z2 to B2:B100, every row would read the options from row 2.
The reliable fix is to give each row its own rule. Doing that by hand is tedious, so use a short Apps Script:
function addDependentDropdowns() {
const sheet = SpreadsheetApp.getActive().getSheetByName('Orders');
for (let row = 2; row <= 100; row++) {
const options = sheet.getRange('E' + row + ':Z' + row);
const rule = SpreadsheetApp.newDataValidation()
.requireValueInRange(options, true)
.setAllowInvalid(false)
.setHelpText('Choose a category in column A first.')
.build();
sheet.getRange('B' + row).setDataValidation(rule);
}
}The true argument to requireValueInRange shows the drop-down arrow. setAllowInvalid(false) rejects values that are not in the range. The script creates 99 separate rules, one per row, each pointing at that row's helper cells. Running the script a second time replaces the rules. It does not add duplicates.
The helper formulas must still exist in E2:E100. Fill them down first.
Edge cases and failure modes
A stale second value
Changing A2 does not clear B2. If B2 holds "Apple" and you change A2 to "Dairy", B2 still shows "Apple". The cell no longer matches the available options, but nothing removes it. Depending on your version of Sheets, the cell may get a red indicator in its corner. Do not count on that as the only protection.
You can catch stale values with a check column. For row 2, put this in C2:
=IF(B2="", "", IF(COUNTIFS(Lists!$A$2:$A, A2, Lists!$B$2:$B, B2)=0, "Re-pick item", "OK"))COUNTIFS counts the rows in Lists where both the category and the item match. A count of 0 means the pair is invalid. You can then colour the check column with conditional formatting to make mismatches visible.
An Apps Script onEdit trigger can also clear B when A changes. That is covered in the chapter on setting validation with Apps Script.
Typed values and spelling
The second rule rejects typed values that are not in the helper range. The check is case-insensitive in practice for drop-down lists, so "apple" is accepted. If you rely on exact capitalisation in later formulas, do not assume it.
Category names that differ
FILTER compares text, and Lists!$A$2:$A=A2 treats "Fruit" and "Fruit " (with a trailing space) as different. A trailing space in the source list gives you a category that appears in the first drop-down but returns no items. When a category returns nothing, check for stray spaces first. You can clean the source with TRIM in a helper column.
Duplicate pairs
If the same category and item appear twice on Lists, the helper range holds that item twice. Wrap FILTER in UNIQUE if duplicates are likely:
=IF(A2="", "", IFERROR(TRANSPOSE(UNIQUE(FILTER(Lists!$B$2:$B, Lists!$A$2:$A=A2))), ""))Blocked spills
Anything typed into F2, G2 or later in the row stops the spill from E2 with a #REF! error. Keep the cells to the right of each helper formula empty. Keep the formulas out of areas other people use for notes.
Pasting over the drop-downs
Pasting a value into B2 can replace the validation rule. Pasting a cell copied from elsewhere replaces the rule with the source cell's rule or with none. Dependent drop-downs have no protection beyond what ordinary validation has. The chapter on the limits of validation covers this.
Design choices
| Approach | Strength | Weakness |
|---|---|---|
| One long list with `FILTER` (this chapter) | Adding an item means adding one row. | Needs helper cells and per-row rules for many rows. |
| One column per category | Easy to read at a glance. | Adding a category means editing the structure and the rules. |
| Apps Script `onEdit` that rebuilds the rule | Can clear stale values and handle many rows. | Needs a script and authorisation. Does not run when the sheet is edited by pasting from outside. |
For a small input form, the helper-cell pattern is enough. For a large table that other people will paste into, consider adding the check column as well.
Exercises
Exercise
A2 holds "Fruit" and B2 holds "Apple". You change A2 to "Dairy". What does B2 show, and how can you detect the problem?
Show answer
B2 still shows "Apple", because changing A2 does not clear B2. Detect it with a check formula such as =IF(B2="", "", IF(COUNTIFS(Lists!$A$2:$A, A2, Lists!$B$2:$B, B2)=0, "Re-pick item", "OK")), which returns "Re-pick item" when the category and item pair does not exist on the Lists sheet.
Exercise
You apply the rule "Dropdown (from a range) Orders!E2:Z2" to B2:B100. Why do rows 3 to 100 show the wrong options, and what is the fix?
Show answer
The range in a drop-down-from-range rule is a fixed reference and is not shifted for each row, so every row reads the options from row 2. The fix is to create one rule per row, for example with an Apps Script loop that calls requireValueInRange on E3:Z3 for B3, E4:Z4 for B4, and so on.