Home › Excel functions › SUBTOTAL
SUBTOTAL Function in Excel: Sum Filtered and Visible Cells
SUBTOTAL calculates a sum, count, average or other statistic for a range, but ignores rows that are hidden by a filter. Filter your list on one region and the total updates to show just that region.
In this guide
SUBTOTAL syntax
=SUBTOTAL(function_num, ref1, [ref2], …)| Argument | What it means |
|---|---|
| function_num | A number that tells SUBTOTAL which calculation to do, such as 9 for SUM or 109 for SUM ignoring hidden rows. See the table below. |
| ref1, ref2, … | The range or ranges to calculate on. |
Function numbers
| Calculation | Includes manually hidden rows | Ignores manually hidden rows |
|---|---|---|
| AVERAGE | 1 | 101 |
| COUNT (numbers) | 2 | 102 |
| COUNTA (non-empty) | 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 |
1–11 vs. 101–111: both versions ignore rows hidden by a filter. The difference is rows you hide yourself (right-click › Hide): 1–11 still include them, 101–111 leave them out. If you're unsure, use 101–111: they always calculate only what you can see.
SUBTOTAL practice: try it yourself
Reading is one thing, typing it is another. Build the formula below step by step: the tips tell you what to type next, and you can click or drag across cells to add them, just like in Excel.
Task: Add up the amounts with SUBTOTAL, in B7: =SUBTOTAL(9,B2:B6)
Example: sum a filtered list
| A | B | C | |
|---|---|---|---|
| 1 | Region | Rep | Sales |
| 2 | North | Anna | 1,200 |
| 3 | South | Ben | 800 |
| 4 | North | Chris | 950 |
| 5 | East | Dana | 450 |
| 6 | South | Anna | 700 |
| 7 | Total | 4,100 |
With no filter, C7 shows 4,100, the same as SUM. Now filter column A on "North" (Data › Filter). Rows 3, 5 and 6 are hidden and the total changes to 2,150: only the visible rows are added. A normal =SUM(C2:C6) would keep showing 4,100.
Count or average visible rows
- Count the visible rows:
=SUBTOTAL(103, A2:A6)counts the non-empty visible cells. After filtering on North, it returns 2. - Average of what's visible:
=SUBTOTAL(101, C2:C6)returns 1,075 for North. - Highest visible value:
=SUBTOTAL(104, C2:C6)returns 1,200 for North.
Tip: put the SUBTOTAL formulas above the list instead of below it. Then they stay visible when the list gets long.
SUBTOTAL ignores other subtotals
If a range contains cells that themselves use SUBTOTAL, those cells are skipped. That means you can add subtotals per group and a grand total over the whole column, without counting anything twice. This is exactly what Excel's built-in Data › Subtotal command does: it inserts SUBTOTAL formulas for each group and one for the grand total. The total row of an Excel table (Ctrl+T, then Table Design › Total Row) uses SUBTOTAL too.
Totals per group with the Subtotal button
Excel also has a Subtotal button that inserts these formulas for you. Sort the list on the column you want to group by, for example Region. Then choose Data > Subtotal, set At each change in to Region, Use function to Sum, and tick the columns to add up. Excel puts a total below each group and a grand total at the bottom, with buttons 1, 2 and 3 on the left to fold the groups in and out. Undo it all with Data > Subtotal > Remove All.
The button is greyed out in a formatted table: convert it to a normal range first (Table Design > Convert to Range).
Sum or count visible rows with a condition
SUBTOTAL has no condition argument, and SUMIFS and COUNTIFS look at hidden rows as well. The easiest fix is a helper column that tells you whether a row is visible. In the example above, put this in D2 and fill it down to D6:
=SUBTOTAL(103, A2)It returns 1 when the row is visible and 0 when the filter hides it. Now add that column as an extra condition:
- Sum for one rep, visible rows only:
=SUMIFS(C2:C6, B2:B6, "Anna", D2:D6, 1) - Count for one rep, visible rows only:
=COUNTIFS(B2:B6, "Anna", D2:D6, 1)
Filter column A on "South" and the sum for Anna becomes 700 instead of 1,900: her North row is hidden. Without a helper column it takes one longer formula: =SUMPRODUCT(SUBTOTAL(103, OFFSET(A2, ROW(A2:A6)-ROW(A2), 0)) * (B2:B6="Anna") * C2:C6).
Number only the visible rows
Normal row numbers get gaps as soon as you filter. A running count with SUBTOTAL renumbers the visible rows 1, 2, 3 every time the filter changes. Put this in the first row of a numbering column and fill it down:
=SUBTOTAL(103, $B$2:B2)The range grows by one row each time ($B$2:B3, $B$2:B4 and so on) and counts only the visible, non-empty cells in it. Point it at a column that is never empty, such as a name or ID column.
Common mistakes
- The total doesn't change when you hide rows manually. You used 1–11. Switch to 101–111.
- Hidden columns are still included. SUBTOTAL only ignores hidden rows. It doesn't skip hidden columns in a horizontal range.
- #VALUE! The function number isn't one of 1–11 or 101–111.
- You need to ignore errors as well. Use AGGREGATE, which works like SUBTOTAL but can also skip error values:
=AGGREGATE(9, 7, C2:C6).
Keep learning
Frequently asked questions
What is the difference between SUBTOTAL 9 and 109?
Both add up only the rows that are visible after filtering. 109 also skips rows you've hidden manually; 9 still includes them.
How do I sum only visible cells in Excel?
Use =SUBTOTAL(109, range). It ignores rows hidden by a filter and rows hidden manually.
Why use SUBTOTAL instead of SUM?
SUM always adds every cell in the range. SUBTOTAL respects filters and skips other subtotals, so it gives the right total for whatever is currently visible.
What is AGGREGATE?
AGGREGATE is a more powerful version of SUBTOTAL, available since Excel 2010. It offers more calculations and can also ignore error values.
How do I copy only the visible rows or the subtotals?
Select the range, choose Home › Find & Select › Go To Special › Visible cells only (or press Alt+;), then copy and paste. For the totals from Data › Subtotal, first click outline button 2 so only the total rows are showing.
Can SUBTOTAL calculate across several sheets?
Not with a 3D reference such as Sheet1:Sheet3!C2:C6; SUBTOTAL doesn't support those. Add one SUBTOTAL per sheet instead: =SUBTOTAL(109, Sheet1!C2:C6) + SUBTOTAL(109, Sheet2!C2:C6).