How-to recipes · Counting and summing
How to sum only visible cells in Excel
SUM adds hidden and filtered rows too. Use SUBTOTAL 109 or AGGREGATE to total only the rows you can see after filtering or hiding.
You filter a sales list to show only the North region, and the total at the bottom still shows the grand total of every region. That is because SUM adds every cell in its range, whether you can see it or not. When you filter, hide rows, or collapse a group, you usually want the total to follow what is on screen. Excel has two functions built for this, SUBTOTAL and AGGREGATE, and they work in every current version, including Microsoft 365.
The example data
Six sales in A1:B7. Without a filter the total is 4320. With the filter Region = North applied, rows 3, 5 and 6 are hidden and the visible amounts are 1200, 800 and 620.
The formula: SUBTOTAL with 109
| A | B | |
|---|---|---|
| 1 | Region | Amount |
| 2 | North | 1200 |
| 3 | South | 450 |
| 4 | North | 800 |
| 5 | East | 300 |
| 6 | South | 950 |
| 7 | North | 620 |
| 8 | ||
| 9 | 2620 |
The grid shows all rows; the result is what B9 displays after filtering Region to North. With no filter it shows 4320, the same as SUM.
Result: 2620
The first argument of SUBTOTAL chooses the calculation. The important detail is the difference between the 1-11 codes and the 101-111 codes:
| Code | Calculation | Rows hidden by a filter | Rows hidden manually |
|---|---|---|---|
| 9 | Sum | excluded | included |
| 109 | Sum | excluded | excluded |
| 1 / 101 | Average | excluded | included / excluded |
| 2 / 102 | Count numbers | excluded | included / excluded |
| 3 / 103 | Count non-empty | excluded | included / excluded |
| 4 / 104, 5 / 105 | Max, Min | excluded | included / excluded |
Filtered-out rows are always ignored. The 100-series codes also ignore rows you hid with right-click > Hide or by collapsing an outline group. Use 109 unless you have a reason not to.
The fastest way to insert it
Filter the data, select the cell under the Amount column and press Alt+= (AutoSum) [Mac: Cmd+Shift+T]. When a filter is active, Excel writes =SUBTOTAL(9,...) instead of SUM. Change 9 to 109 if rows may also be hidden manually. Excel tables do the same: turn on Table Design > Total Row and the total uses SUBTOTAL 109 automatically (Excel tables and structured references).
AGGREGATE: visible cells and no errors
AGGREGATE (Excel 2010 and later) is a stronger version of SUBTOTAL. It can also skip error values, which would otherwise break the total:
=AGGREGATE(9,5,B2:B7)Function 9 is SUM and option 5 means ignore hidden rows. Option 7 ignores hidden rows and error values, which is handy when some amounts are #N/A from a failed lookup. SUM and SUBTOTAL would return the error.
Visible cells that also meet a condition
Sometimes you need the visible rows that also match a condition, for example the visible rows with an amount over 700. SUMIFS cannot do this because it ignores filtering. The classic solution works in all versions:
=SUMPRODUCT(SUBTOTAL(103,OFFSET(B2,ROW(B2:B7)-ROW(B2),0))*(B2:B7>700)*B2:B7)OFFSET(B2,ROW(B2:B7)-ROW(B2),0)hands SUBTOTAL each cell of the column separately.SUBTOTAL(103, ...)returns 1 for a visible non-empty cell and 0 for a hidden one, so you get an array of visibility flags.- Multiplying by the condition and the amounts and adding with SUMPRODUCT gives the total of the visible rows that meet the condition.
With the North filter active it returns 2,000 (1200 + 800); 620 is visible but not over 700. It is volatile and slower on big data, so use it only when needed.
Pitfalls
- SUBTOTAL ignores other SUBTOTAL and AGGREGATE formulas inside its range, which prevents double counting of group subtotals. SUM does not.
- SUBTOTAL works on rows only. Hidden columns are always included, even with 109, so
=SUBTOTAL(109,B2:H2)across a row ignores nothing. - Copy and paste of a filtered range already copies only visible cells, but for a manually hidden range press Alt+; (Go To Special > Visible cells only) before copying.
- If the total does not change when you filter, check that the formula is not inside the filtered range itself, and that you used SUBTOTAL rather than SUM.