SUM Includes Filtered Rows: Use SUBTOTAL Instead
When a filter hides rows, SUM still includes them. In Google Sheets, SUBTOTAL 9 follows the filter; 109 also leaves out rows hidden by hand.
本文目录
Six rows of a Saturday farmers-market tally are hidden by a filter, but the SUM at the bottom still shows 748.00. The three visible Produce rows add up to 232.00. This is a wrong total, not a # error. The 748.00 与 232.00 figures, and the 9 versus 109 table later on, were measured in Google Sheets. I did not re-check Excel AutoFilter, rows hidden by hand, or a table total row.
Put SUM and SUBTOTAL beside each other
Enter this fictional stall list in A1:D10. Store the amounts as numbers and format D2:D10 to two decimal places.
| Stall | Item | 分类 | Amount |
|---|---|---|---|
| M-01 | Sourdough | Bakery | 86.50 |
| M-02 | Carrots | Produce | 72.00 |
| M-03 | Strawberry jam | Preserves | 54.50 |
| M-04 | Croissants | Bakery | 91.00 |
| M-05 | Tomatoes | Produce | 84.00 |
| M-06 | Apple butter | Preserves | 63.00 |
| M-07 | Rye bread | Bakery | 78.00 |
| M-08 | Potatoes | Produce | 76.00 |
| M-09 | Pickled onions | Preserves | 143.00 |
Filter A1:D10 so that only Produce remains in column C. Rows 3, 6 and 9 stay visible (72.00, 84.00 与 76.00).
In D12:
=SUM(D2:D10)
In D13:
=SUBTOTAL(9,D2:D10)
With the Produce filter on, SUM is still 748.00. SUBTOTAL returns 232.00. SUM adds every numeric cell in D2:D10 whether the row is visible or not. The filter only changes what you see. SUBTOTAL follows the filter.
Function numbers 9 and 109, measured in Sheets
Add a third formula in D14:
=SUBTOTAL(109,D2:D10)
Both 9 and 109 are sums. The difference is how they treat rows you hide by hand. These three states were measured in Google Sheets (I did not re-check Excel on a Mac):
| Formula | All rows visible | Produce filter | Rows hidden by hand |
|---|---|---|---|
=SUM(D2:D10) | 748.00 | 748.00 | 748.00 |
=SUBTOTAL(9,D2:D10) | 748.00 | 232.00 | 748.00 |
=SUBTOTAL(109,D2:D10) | 748.00 | 232.00 | 232.00 |
For the manual-hide column, the filter was cleared first, then rows 2, 4, 5, 7, 8 and 10 were hidden from the row headers so the same three Produce rows remained visible. In Sheets, function 9 follows a filter but still adds manually hidden rows. Function 109 leaves out both. If the footer should match whatever is visible, including rows hidden by hand, I would use 109. If a manually hidden stall must stay in the total, use 9. SUM is the wrong footer once a filter is supposed to change the total.
Check for existing subtotals inside the range
A second problem appears when the Amount column already contains an intermediate total. Restore every data row. In D11:
=SUBTOTAL(9,D2:D4)
That returns 213.00 (sourdough, carrots and jam). An outer formula that includes D11:
=SUBTOTAL(9,D2:D11)
returns 748.00. SUBTOTAL skips other SUBTOTAL results inside its range, so the first three amounts are not added twice.
Replace D11 with =SUM(D2:D4), still 213.00 on its own. The same outer =SUBTOTAL(9,D2:D11) then returns 961.00. The inner SUM is just another number, so those three stalls are counted twice. When a filtered total looks too high, look at the formulas inside the range, not only the displayed values.
When the total should not follow the filter
If you want the Produce total no matter what is hidden, use a category condition instead of SUBTOTAL:
=SUMIF(C2:C10,"Produce",D2:D10)
That returned 232.00 with no filter, with the Produce filter on, and with the same rows hidden by hand. If a SUMIF is unexpectedly zero, see SUMIF and SUMIFS returning zero for criteria and text numbers. For a separate list of matching stalls rather than a footer under the same list, see FILTER vs QUERY. A total that is off by a cent because of rounded tax is a different problem, covered in why a rounded total is off by a cent.
\n