Category: FUNCTION | Updated: 2026-11-22
How to Use the SUBTOTAL Function in Google Sheets
Syntax
=SUBTOTAL(function_code, range1, [range2, ...])
- function_code: A number specifying the aggregation method to perform (common codes:
1= AVERAGE,2= COUNT,3= COUNTA,9= SUM,109= SUM excluding hidden rows). - range1: The cell range to aggregate.
- range2, … (optional): Additional cell ranges to aggregate.
Example Table
When you filter to show only “Tokyo,” the hidden “Osaka” row is excluded and the total (5000) is calculated automatically.
| Column A (Branch) | Column B (Sales) | Notes | |
|---|---|---|---|
| Row 1 | Branch | Sales | |
| Row 2 | Tokyo | 3000 | Visible |
| Row 3 | (Osaka) | (4000) | Hidden by filter |
| Row 4 | Tokyo | 2000 | Visible |
| Row 5 | Total | 5000 | =SUBTOTAL(9, B2:B4) |
Common Errors and Caveats
- Hidden columns are not excluded: The
SUBTOTALfunction accounts for hidden or filtered rows, but if you hide columns, values in those hidden columns are still included in the calculation. - Prevents double-counting subtotals: If there are subtotal rows created with another
SUBTOTALfunction inside your target range, they are automatically ignored so the total is not doubled.
Frequently Asked Questions
Q. What is the difference between function codes 9 and 109 in the SUBTOTAL function?
A. Both calculate the sum (SUM), but 9 includes manually hidden rows in the calculation, whereas 109 excludes both filtered-out rows and manually hidden rows.
Q. What is the biggest advantage of using SUBTOTAL instead of SUM?
A. When you filter your data, SUBTOTAL automatically updates to calculate the sum or count of only the visible rows, and it avoids double-counting any other SUBTOTAL results within the range.