Sub topic 10
Total only the visible rows with SUBTOTAL
This chapter assumes you can apply an AutoFilter, enter basic formulas, and read a cell reference like D2:D100. You should also know that filtering hides rows without deleting them.
Why SUM gives the wrong total on a filtered list
When you filter a list, Excel hides the rows that do not match. The hidden rows are still in the worksheet. SUM, AVERAGE, COUNT, and the other ordinary functions read every cell in the range, hidden or not.
Suppose column D holds order amounts and you filter to Region = West. A cell with =SUM(D2:D100) still returns the total for all regions. The number on screen no longer matches the rows you see.
SUBTOTAL fixes this. It calculates over a range, and it can skip rows that are hidden.
The SUBTOTAL syntax
=SUBTOTAL(function_num, ref1, [ref2], ...)function_numis a number that selects which calculation to run, such as sum, average, or count.ref1,ref2, and so on are the ranges to calculate over. You can pass up to 254 of them.
The key detail is that function_num comes in two ranges, and the range you choose decides which hidden rows are skipped.
| Calculation | Number (includes manually hidden rows) | Number (ignores manually hidden rows) |
|---|---|---|
| AVERAGE | 1 | 101 |
| COUNT | 2 | 102 |
| COUNTA | 3 | 103 |
| MAX | 4 | 104 |
| MIN | 5 | 105 |
| PRODUCT | 6 | 106 |
| STDEV | 7 | 107 |
| STDEVP | 8 | 108 |
| SUM | 9 | 109 |
| VAR | 10 | 110 |
| VARP | 11 | 111 |
Two rules cover everything:
- Rows removed by a filter are always ignored, whichever number you use.
- Rows hidden by hand (right-click, Hide) are ignored only by the 101 to 111 numbers. The 1 to 11 numbers still count them.
The formula from the plan uses 109:
=SUBTOTAL(109, D2:D100)This sums column D and ignores rows hidden by a filter and rows hidden manually, so the total matches what is visible.
A worked example
Take a small sales list in A1:D8. Row 1 holds the headers.
| Row | A: Rep | B: Region | C: Product | D: Amount |
|---|---|---|---|---|
| 1 | Rep | Region | Product | Amount |
| 2 | Ana | West | Desk | 600 |
| 3 | Ben | East | Chair | 250 |
| 4 | Cara | West | Lamp | 150 |
| 5 | Dev | North | Desk | 900 |
| 6 | Eli | West | Chair | 500 |
| 7 | Fay | East | Lamp | 100 |
| 8 | Gus | North | Chair | 300 |
Put the totals in row 10, below the data and with one blank row between. Use these cells:
=SUM(D2:D8)
=SUBTOTAL(109, D2:D8)
=SUBTOTAL(103, A2:A8)With no filter, the first two both return 2800, and the third returns 7 (the count of non-empty names).
Now filter Region to West. Three rows remain: Ana (600), Cara (150), and Eli (500).
=SUM(D2:D8)still returns 2800.=SUBTOTAL(109, D2:D8)returns 1250.=SUBTOTAL(103, A2:A8)returns 3.
The SUBTOTAL results follow the filter. Clear the filter and they return to 2800 and 7.
Where to put the formula
Place the SUBTOTAL cell where a filter cannot hide it.
- Put it above the data, in a row over the headers, or in a separate area. A filter on the list never hides rows above the header row.
- If you put it directly below the data, leave at least one blank row. Otherwise Excel may treat the total row as part of the list, and filtering or sorting can pull it in or hide it.
- If the total sits in a row inside the filtered range, a filter that excludes that row hides your total.
Totals in an Excel Table
If your list is an Excel Table, you do not need to type the formula. Turn on Table Design > Total Row. Excel adds a row at the bottom of the table and fills it with a SUBTOTAL formula. You can pick Sum, Average, Count, Max, Min, and others from a drop-down in each cell.
The generated formula uses the 100-series numbers and structured references. For a table named Sales with an Amount column, it looks like this:
=SUBTOTAL(109, [Amount])The total row sits inside the table but outside the filter range, so filtering never hides it. Chapter material on Tables covers this feature in more detail. Here the point is that it is the same function.
Counting visible rows
To report how many rows survive a filter, use a count function number.
=SUBTOTAL(103, A2:A100)103 is COUNTA with manually hidden rows ignored. It counts non-empty cells, so point it at a column that is always filled in, such as an ID or a name. If the column has blanks, those rows are not counted.
Use 102 (COUNT) only on a numeric column. It counts numbers, not text.
For a message such as "3 of 7 rows shown", combine two calls:
=SUBTOTAL(103, A2:A8) & " of " & COUNTA(A2:A8) & " rows shown"Gotchas and edge cases
Nested SUBTOTAL formulas are skipped
If the range contains other SUBTOTAL formulas, SUBTOTAL ignores them. This prevents double counting. It is the reason a grand total built with SUBTOTAL over a range of group subtotals gives the correct figure. It also means you cannot use SUBTOTAL to add up cells that are themselves SUBTOTAL results, as they are excluded.
Only rows are skipped, not columns
SUBTOTAL is designed for rows (vertical ranges). Hiding a column does not exclude its cells for any function number. If you pass a horizontal range, such as A2:D2, a hidden column is still counted.
1 to 11 versus 101 to 111
Many people use 9 out of habit and then see a total that includes rows they hid by hand. Decide which behavior you want:
- If the rows are hidden only by a filter, both series work the same.
- If users may also hide rows manually and you want the total to match the screen, use the 100 series.
Hidden rows inside a Group or outline
Rows collapsed with Data > Group count as hidden for the 100 series. They are included by the 1 to 11 series.
Calculations are not recalculated by hiding alone in older behavior
In current Excel, hiding or filtering rows recalculates SUBTOTAL right away. If a total looks stale, check that calculation is set to Automatic under Formulas > Calculation Options.
Errors in the range
If any visible cell in the range holds an error such as #N/A, SUBTOTAL returns that error. Errors in rows that are filtered out do not matter.
Text numbers
Numbers stored as text are ignored by sum, average, and count-of-numbers calculations. If your visible total is lower than expected, check the column for left-aligned values or green triangle warnings. The pitfalls chapter covers the fix.
SUBTOTAL compared with AGGREGATE
Excel also has AGGREGATE, which does the same job with more options. It can ignore errors as well as hidden rows.
=AGGREGATE(9, 5, D2:D100)The first argument is the function (9 is SUM), and the second is an option (5 means ignore hidden rows). Choose SUBTOTAL for ordinary filtered lists because it is simple and widely known. Choose AGGREGATE when the visible rows may contain errors you want to skip.
Decision guide
| Situation | Use |
|---|---|
| Total of a filtered list, rows hidden by filter only | `SUBTOTAL(109, range)` |
| Total that must also skip hand-hidden rows | `SUBTOTAL(109, range)` (100 series) |
| Total that deliberately includes hand-hidden rows | `SUBTOTAL(9, range)` |
| Visible row count | `SUBTOTAL(103, range)` |
| Visible total where errors may appear | `AGGREGATE(9, 5, range)` |
| Total in an Excel Table | Table Design > Total Row |
Exercise
A list in A2:D100 is filtered to Region = West. A colleague also right-clicked and hid rows 10 to 12 by hand, and those rows are West rows. You want a total of column D that matches exactly what you see on screen. Which formula do you use, and why would =SUBTOTAL(9, D2:D100) give a different answer?
Show answer
Use =SUBTOTAL(109, D2:D100). The 100-series numbers ignore rows hidden by hand as well as rows hidden by the filter. SUBTOTAL(9, ...) ignores filtered rows but still counts rows 10 to 12, because they were hidden manually, so its total would be larger than the visible amounts.
Exercise
You put =SUBTOTAL(109, D2:D8) in D9, directly under the data, and then filter on Region. The total sometimes disappears. Give two ways to change the layout so it always stays visible.
Show answer
Move the formula above the headers, for example into D1 with the headers in row 3, because filters never hide rows above the header row. Alternatively, leave a blank row between the data and the total, or convert the list to an Excel Table and use the Total Row, which sits outside the filtered range.