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.

StallItem分類Amount
M-01SourdoughBakery86.50
M-02CarrotsProduce72.00
M-03Strawberry jamPreserves54.50
M-04CroissantsBakery91.00
M-05TomatoesProduce84.00
M-06Apple butterPreserves63.00
M-07Rye breadBakery78.00
M-08PotatoesProduce76.00
M-09Pickled onionsPreserves143.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.

Google Sheets Produce filter leaving three stall rows visible; SUM in D12 is 748.00, while SUBTOTAL 9 in D13 and SUBTOTAL 109 in D14 are both 232.00.
Screenshot: Google Sheets, Nov 2026

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):

FormulaAll rows visibleProduce filterRows hidden by hand
=SUM(D2:D10)748.00748.00748.00
=SUBTOTAL(9,D2:D10)748.00232.00748.00
=SUBTOTAL(109,D2:D10)748.00232.00232.00
Google Sheets with the same three Produce rows visible after other rows were hidden by hand; SUM and SUBTOTAL 9 remain 748.00, while SUBTOTAL 109 is 232.00. Filter icons show no criteria, and hidden-row arrows appear in the row headers.
Screenshot: Google Sheets, Nov 2026

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

Oscar

寫 Excel 與 Google Sheets 的公式和排錯。

Google Sheets, 26 November 2026, new private unshared spreadsheet (locale en_US, 1000-row grid, America/New_York). Formulas were entered as user input through the Sheets API. Amounts in D2:D10 formatted 0.00. Unfiltered, SUM(D2:D10), SUBTOTAL(9,D2:D10) and SUBTOTAL(109,D2:D10) each returned 748.00. SUMIF of Produce returned 232.00. With a basic filter showing only Produce (rows 3, 6 and 9 visible): SUM remained 748.00; SUBTOTAL 9 and SUBTOTAL 109 each returned 232.00; SUMIF Produce remained 232.00. Filter cleared, then rows 2, 4, 5, 7, 8 and 10 hidden with hiddenByUser (same three Produce rows visible): SUM 748.00, SUBTOTAL 9 748.00, SUBTOTAL 109 232.00, SUMIF Produce 232.00. On a clean Amount copy, SUBTOTAL(9,F2:F4) returned 213; SUBTOTAL(9,F2:F11) with that nested SUBTOTAL in F11 returned 748 (the nested SUBTOTAL was skipped). After F11 was changed to SUM(F2:F4)=213, SUBTOTAL(9,F2:F11) returned 961 (the nested SUM was included). Excel Table totals, AGGREGATE, and Excel's 9 versus 109 behavior were not run (Needs Mac).

本主題下一篇:表格錯誤與動態陣列 Compound Interest Formula in Excel and Google Sheets