Sub topic 14
Next step: SUBTOTAL for filtered and hidden rows
This chapter assumes you can write SUM, AVERAGE, MIN, MAX, and COUNT formulas over a range. You should also know how blanks, text, and errors affect them, and how to filter a table with the Filter button on the Data tab.
The problem: SUM ignores your filter
SUM, AVERAGE, MIN, MAX, and COUNT always read every cell in the range. They do not care whether a row is hidden. Suppose you filter a sales table to show only the East region. =SUM(B2:B6) still adds all five rows, including the ones you can't see. The total on screen no longer matches the rows you are looking at.
SUBTOTAL fixes this. It takes a function number that picks the calculation, then one or more ranges.
=SUBTOTAL(109, B2:B6)Here 109 means SUM, and rows hidden or filtered out are left out of the result.
Anatomy of SUBTOTAL
The syntax is =SUBTOTAL(function_num, ref1, [ref2], ...).
Each calculation has two function numbers. The 1–11 series includes rows you hid by hand. The 101–111 series ignores them. Both series skip rows removed by a filter.
| Calculation | Includes manually hidden rows | Ignores manually hidden rows |
|---|---|---|
| AVERAGE | 1 | 101 |
| COUNT | 2 | 102 |
| COUNTA | 3 | 103 |
| MAX | 4 | 104 |
| MIN | 5 | 105 |
| SUM | 9 | 109 |
In practice, use the 100-series numbers (101–111) unless you have a reason not to. They match what most people expect from "only what I can see."
=SUBTOTAL(101, B2:B6)
=SUBTOTAL(102, B2:B6)
=SUBTOTAL(104, B2:B6)
=SUBTOTAL(105, B2:B6)
=SUBTOTAL(109, B2:B6)These return the average, count, maximum, minimum, and sum of the visible numeric cells.
Worked example
Take this table in A1:B6.
| Region | Sales |
|---|---|
| East | 120 |
| West | 80 |
| East | 150 |
| West | 60 |
| East | 90 |
Put the formulas in D1 and D2:
=SUM(B2:B6)
=SUBTOTAL(109, B2:B6)Before filtering, both return 500. Now filter Region to show only East. The SUM formula still returns 500. The SUBTOTAL formula returns 360, which is 120 + 150 + 90, the sales you can see.
Place the formula outside the filtered range, such as above the table or in another column. If it sits inside a row that gets filtered out, it disappears with that row.
Gotchas
- Rows hidden by a filter are always excluded. Even function number 9 skips them. The difference between the two series only matters for rows you hide with right-click, Hide.
- Hidden columns are not affected. SUBTOTAL only looks at rows. Hiding a column does not change the result for either series.
- Nested SUBTOTALs are ignored. If the range contains other SUBTOTAL formulas, SUBTOTAL skips them. This prevents double counting in grouped reports.
- Errors still pass through. A visible cell that contains
#N/Amakes the SUBTOTAL result an error, as it would with SUM. - Empty visible data still fails for averages.
=SUBTOTAL(101, B2:B6)returns#DIV/0!when a filter leaves no visible numbers. Guard it as you would AVERAGE:
=IF(SUBTOTAL(102, B2:B6)=0, "No data", SUBTOTAL(101, B2:B6))When to use which
Use SUM, AVERAGE, and the others when you want every row counted. Use SUBTOTAL when the table is filtered or rows are hidden and the result must match what is visible. Use SUMIF, AVERAGEIF, or COUNTIF when you want a condition written into the formula so the result does not depend on any filter state.
Exercise
A table in A1:B20 is filtered to show only some rows. You hid row 7 by hand before filtering. Which formula returns the largest Sales value among the rows you can see, and does it include row 7? Assume Sales is in B2:B20.
Show answer
Use =SUBTOTAL(104, B2:B20). Function number 104 is MAX in the series that ignores manually hidden rows, so row 7 is excluded, as are the filtered-out rows. Function number 4 would also exclude filtered rows but would include row 7.
Exercise
Write a formula that returns the average of the visible numbers in B2:B20, or the text "No data" when no numbers are visible.
Show answer
=IF(SUBTOTAL(102, B2:B20)=0, "No data", SUBTOTAL(101, B2:B20))