Sub topic 14
Spill ranges in data validation, charts and formatting
This chapter assumes you can create a spilling formula, reference it with #, and recognize #SPILL!. Those topics come from earlier chapters. Here you will connect a spill range to three features that don't recalculate formulas but still need to know how big a range is: drop-down lists, charts and conditional formatting.
Why these features need extra care
A spilling formula changes size when its inputs change. A formula that returns 6 regions today may return 9 next month. Cells, formulas and functions follow that change automatically. Data validation, chart series and formatting rules store a range address, and a fixed address like F2:F7 does not grow.
The fix is the same each time. Point the feature at the spill range, not at a fixed block of cells. There are two ways to do that:
- Use the
#operator directly, where the feature accepts it. - Define a name that refers to the spill range, then use the name in the feature.
The name approach is the more dependable one, because names are accepted almost everywhere a range is.
Sample data
The examples use a sales list in A1:C20. Row 1 holds the headers Region, Product and Amount. Rows 2 to 20 hold the data.
Put this formula in F2 to get a sorted list of distinct regions:
=SORT(UNIQUE(A2:A20))The anchor is F2, and the whole list is F2#.
Data validation: drop-down lists that resize
A data validation list normally takes a range such as $F$2:$F$7. If a new region appears, the drop-down misses it. Use the spill reference instead.
- Select the cell that should show the drop-down, for example
H1. - Choose Data > Data Validation.
- Set Allow to List.
- In Source, enter
=$F$2#. - Select OK.
The drop-down now lists whatever F2 currently spills. When the data gains a region, the spill grows and the list grows with it. No one edits the validation rule.
Some limits to keep in mind:
- The spill must be one-dimensional, a single column or a single row. A two-dimensional spill does not work as a list source.
- The anchor must hold a formula that actually spills. If
F2shows#SPILL!, the list is not usable until you fix the error. - If the validation is on a different sheet, include the sheet name, for example
=Data!$F$2#.
The same list through a name works too. Create a name RegionList that refers to =Sheet1!$F$2#, then enter =RegionList as the Source. Use a name when several cells share the list, because you can change the definition in one place.
A dependent drop-down
A dependent list changes with another choice. Suppose H1 holds a region chosen from the list above, and H2 should offer only the products sold in that region. In J2, enter:
=SORT(UNIQUE(FILTER(B2:B20,A2:A20=H1,"")))Then set the validation Source for H2 to =$J$2#.
The third argument of FILTER, "", matters here. If no rows match, for example while H1 is still empty, FILTER would otherwise return #CALC!, and the drop-down would break. With "", the spill is one blank cell and the list is simply empty.
Charts: series that follow the spill
A chart series is stored as a range address too. Suppose you want a column chart of total sales by region. Put the regions and totals in two spilling formulas:
=UNIQUE(A2:A20)in F2, and in G2:
=SUMIFS(C2:C20,A2:A20,F2#)The criteria argument of SUMIFS receives the whole spill from F2, so the formula returns one total per region and spills beside the region list.
A chart built on F2:G7 shows six regions. If a seventh region appears, the spills grow to row 8, but the chart still points at row 7. To make the chart follow:
- Open Formulas > Name Manager and select New.
- Name it
ChartRegionsand set Refers to=Sheet1!$F$2#. - Create a second name,
ChartTotals, that refers to=Sheet1!$G$2#. - Right-click the chart and choose Select Data.
- Edit the series. Set Series values to the file name, an exclamation mark and the name, for example
=Sales.xlsx!ChartTotals. - Edit the horizontal axis labels and set them to
=Sales.xlsx!ChartRegions.
Use your own workbook file name. If you type the sheet name instead (=Sheet1!ChartTotals), Excel rewrites it to the file name form for a workbook-level name. A name must be in the series formula this way, because Excel does not accept a bare name in the series box.
Now add a row of data for a new region. Both spills grow, both names grow, and the chart adds a column.
Two further points about charts:
- Keep the label spill and the value spill the same length. If the totals spill is 7 cells and the labels spill is 6, the chart shows an unlabeled point.
- Sort inside the formula, not by sorting the cells. For a ranked chart, return the sorted table with
SORTBYand split the columns with names, or sort both columns from the same ordering formula.
Conditional formatting: rules that cover a changing range
A conditional formatting rule has two parts: the cells it applies to, and the formula or condition. Spills can affect either part.
Highlight source rows that appear in a spill
Suppose a spill in L2 lists the top three regions by sales, and you want to highlight those regions in the data. Define a name TopRegions that refers to =Sheet1!$L$2#. Select A2:A20, then choose Home > Conditional Formatting > New Rule > Use a formula. Enter:
=COUNTIF(TopRegions,A2)>0The reference A2 is relative to the first cell of the selection, so the rule tests every row. When the top three change, the highlighting changes.
Format the spilled cells themselves
Formatting the output of a spill is harder. The cells that a spill covers change, but the Applies to range of the rule does not follow. Options:
- Apply the rule to a generous fixed range, for example
F2:G200, and write the formula so that empty cells are ignored. A rule such as=AND(F2<>"",F2>100)leaves cells below the spill alone, because they are empty. - Test whether your version of Excel accepts a spill reference in the Applies to box. Some versions do. If you use it, check that the format still follows when the spill grows and shrinks.
The generous fixed range is the safe choice. It works in every version that supports dynamic arrays.
Ordinary cell formats and tables
Ordinary formats, such as number format, fill and borders, belong to cells and not to the formula. When a spill shrinks, the formats on the cells it left stay. If you format F2:F7 as currency and the spill later covers F2:F9, the two new cells keep their old format. Format the column, or use conditional formatting, so the format covers every cell the spill might reach.
Spills also cannot go inside an Excel table. A dynamic array formula in a table cell returns #SPILL!. Keep the formulas outside the table and reference the table columns, for example =UNIQUE(Sales[Region]).
Summary of approaches
| Feature | Accepts `#` directly | Dependable approach |
|---|---|---|
| Data validation list | Yes, as `=$F$2#` | `=$F$2#` or a defined name |
| Chart series | Not reliably | Defined name that refers to the spill |
| Conditional formatting formula | Not reliably | Defined name used in the formula |
| Conditional formatting Applies to | Version dependent | Generous fixed range and a rule that ignores blanks |
| Cell number format | No | Format the whole column |
Exercise
A drop-down in H1 uses the Source =$F$2#. F2 holds =SORT(UNIQUE(A2:A20)). Someone types a value into F5, which sits inside the current spill. What happens to the drop-down, and how do you restore it?
Show answer
The formula in F2 returns #SPILL! because F5 is not empty, so the spill cannot expand, and the drop-down has no usable list. Clear F5 (or move the typed value away) so that the spill range is free. The spill returns and the drop-down works again.
Exercise
A chart plots ChartTotals against ChartRegions. After you add a new region to the data, the chart shows an extra column with no label. Name the likely cause and a check you can make.
Show answer
The two spills have different lengths, or the label name points at a fixed range instead of the spill. Select each spill's anchor cell and compare the spill sizes. Open Name Manager and confirm that both names end in #, so both follow their spills.
Exercise
You want to shade every row of A2:C20 whose region appears in the spill at L2. Write the name definition and the rule formula for the selection A2:C20, so that the whole row is shaded.
Show answer
Define TopRegions as =Sheet1!$L$2#. Select A2:C20 and use the formula =COUNTIF(TopRegions,$A2)>0. The dollar sign before A keeps the test on column A, so every cell in the row is shaded.