Sub topic 14
Next step: SUBTOTAL for filtered and hidden rows
This chapter assumes you can write SUM, AVERAGE, MIN, MAX, and COUNT over a range. It also assumes you know how to apply a filter with the Filter button on the Data tab and how to hide a row. SUBTOTAL is the usual next step when those five functions return numbers that don't match what is on screen.
The problem: plain functions ignore the filter
SUM, AVERAGE, MIN, MAX, and COUNT read every cell in the range, whether the row is visible or not. Filtering hides rows from view, but it doesn't remove them from the calculation.
Here is a small data set used throughout the chapter.
| Row | A (Region) | B (Sales) |
|---|---|---|
| 2 | East | 120 |
| 3 | West | 80 |
| 4 | East | 200 |
| 5 | West | 50 |
| 6 | East | 150 |
Suppose you filter column A to show only East. Rows 3 and 5 disappear from view. A formula like this still returns 600, the total of all five rows:
=SUM(B2:B6)A report reader sees three rows of 120, 200, and 150, and a total of 600. The visible rows add up to 470, so the total looks wrong.
SUBTOTAL syntax
SUBTOTAL takes a function number, followed by one or more references:
=SUBTOTAL(109, B2:B6)The first argument selects which calculation to run. The remaining arguments are the ranges to calculate over. SUBTOTAL skips rows that a filter has hidden. The function number decides whether it also skips rows you hid by hand.
With the East filter applied, the formula above returns 470. Clear the filter and it returns 600 again.
Function numbers
Each calculation has two numbers, one in the range 1 to 11 and one in the range 101 to 111. The five functions from this tutorial map like this:
| Calculation | Includes manually hidden rows | Ignores manually hidden rows |
|---|---|---|
| AVERAGE | 1 | 101 |
| COUNT (numbers) | 2 | 102 |
| COUNTA (non-empty cells) | 3 | 103 |
| MAX | 4 | 104 |
| MIN | 5 | 105 |
| SUM | 9 | 109 |
The full list also covers PRODUCT (6 and 106), STDEV (7 and 107), STDEVP (8 and 108), VAR (10 and 110), and VARP (11 and 111).
The two groups differ only in how they treat rows hidden by hand, either with Hide Row or by dragging the row height to zero. Rows hidden by a filter are skipped by both groups.
Worked example with all three cases
Put the totals outside the data, for example in B8 with a blank row 7 between them. A total placed in the row directly under the data can be pulled into the filter range and hidden.
=SUM(B2:B6)
=SUBTOTAL(9, B2:B6)
=SUBTOTAL(109, B2:B6)The three formulas give different results depending on what is hidden:
| Situation | SUM | SUBTOTAL 9 | SUBTOTAL 109 |
|---|---|---|---|
| Nothing hidden | 600 | 600 | 600 |
| Filter Region = East | 600 | 470 | 470 |
| Row 4 hidden by hand | 600 | 600 | 400 |
| Filter East and row 4 hidden by hand | 600 | 470 | 270 |
In the last row, the filter removes rows 3 and 5 for both SUBTOTAL variants. Only 109 also removes row 4, which you hid by hand.
Which group should you use?
Most people want the 100-series. If a row is hidden, the reader usually doesn't expect it to be in the total, whatever the reason for hiding it.
Use the 1-series when you hide rows to tidy the screen but want the total to stay complete. A summary that collapses detail rows is a common case.
Whichever group you pick, use it consistently within a workbook. A mix of 9 and 109 makes two totals disagree after someone hides a row, and the cause is hard to see.
Other calculations under filters
The same pattern works for the other functions you have learned. With the East filter applied, these formulas read only the three visible rows:
=SUBTOTAL(101, B2:B6)
=SUBTOTAL(102, B2:B6)
=SUBTOTAL(103, A2:A6)
=SUBTOTAL(104, B2:B6)
=SUBTOTAL(105, B2:B6)The results are 156.67 (average of 120, 200, 150), 3, 3, 200, and 120.
COUNTBLANK has no SUBTOTAL equivalent. To count empty visible cells, compare a visible count with the total number of rows, or use AGGREGATE, a related function that adds more options.
Behavior to know
Nested SUBTOTALs are skipped
If a range contains other SUBTOTAL formulas, SUBTOTAL ignores them. This prevents double counting. Suppose you have subtotals for each region and a grand total below them. The grand total can cover the whole column, and each region's subtotal is counted once through its detail rows.
=SUBTOTAL(109, B2:B20)The Subtotal command on the Data tab relies on this. It inserts SUBTOTAL rows at each group change and a grand total at the bottom.
Errors pass through
If any visible cell in the range holds an error such as #N/A, SUBTOTAL returns that error, just like SUM does. The cell can be hidden by a filter to avoid the error, but only because SUBTOTAL doesn't read hidden rows. Hiding the row is not a fix for bad data.
Empty results
If a filter leaves no matching rows, SUBTOTAL(109, ...) returns 0, and so do the COUNT variants. SUBTOTAL(101, ...) returns #DIV/0! because there is nothing to average. Guard it the same way as in the error-handling chapter, using the visible count:
=IF(SUBTOTAL(102, B2:B6)=0, "No data", SUBTOTAL(101, B2:B6))Hidden columns are not handled
SUBTOTAL works on rows only. Hiding a column does not remove its cells from a horizontal range, even with a 100-series number. Use another approach, such as AGGREGATE or a helper row, when columns need to be excluded.
3-D references fail
SUBTOTAL doesn't accept 3-D references that span several sheets, such as Sheet1:Sheet3!B2. A formula like that returns #VALUE!.
Excel Tables use SUBTOTAL for you
If your data is in an Excel Table, turn on the Total Row from the Table Design tab. The cell in the total row uses SUBTOTAL, and you pick the calculation from a drop-down list. A SUM total row, for example, looks like this:
=SUBTOTAL(109, [Sales])The drop-down writes the 100-series numbers, so filtering the table updates the totals without extra work.
Quick comparison
| Need | Formula |
|---|---|
| Total of everything, regardless of filter | SUM |
| Total of visible rows after filtering | SUBTOTAL 9 or 109 |
| Total that also drops rows hidden by hand | SUBTOTAL 109 |
| Total of visible rows meeting a condition | SUMIF is not enough on its own, because it ignores filters. Combine a helper column or use AGGREGATE or SUMPRODUCT with SUBTOTAL |
| Average of visible rows | SUBTOTAL 101 |
| Count of visible numbers | SUBTOTAL 102 |
Common mistakes
- Using SUM for a total that should follow the filter. The number never changes when the filter does.
- Using function number 9 and expecting manually hidden rows to be dropped. Switch to 109.
- Placing the total directly beneath the data, where the filter can hide it. Leave a blank row, or put the total above the data.
- Using the 100-series for hidden columns. It only affects rows.
- Forgetting the empty-set case for AVERAGE and wrapping the formula too late. A filter that returns no rows gives #DIV/0! for 101.
Exercise
Using the table in this chapter (East 120, West 80, East 200, West 50, East 150 in rows 2 to 6), you apply a filter for Region = West and also hide row 3 by hand. What do =SUBTOTAL(9, B2:B6) and =SUBTOTAL(109, B2:B6) return, and what does =SUBTOTAL(101, B2:B6) return?
Show answer
The filter leaves rows 3 and 5 (West 80 and West 50). SUBTOTAL 9 includes manually hidden rows, so it returns 80 + 50 = 130. SUBTOTAL 109 drops row 3, so it returns 50. SUBTOTAL 101 averages the remaining visible row and returns 50.
Exercise
Write a formula that returns the average of the visible numbers in B2:B6 after filtering, or the text "No data" when the filter leaves no numeric rows. Ignore rows hidden by hand as well.
Show answer
=IF(SUBTOTAL(102, B2:B6)=0, "No data", SUBTOTAL(101, B2:B6))