Going Stellar

Home/Google Sheets reference

Google Sheets function · Math

SUBTOTAL in Google Sheets: syntax and examples

SUBTOTAL applies an aggregate chosen by a code: 9 for sum, 1 for average, 4 for max, 3 for count of non-empty cells. Codes 101 to 111 do the same and also skip rows you hid by hand. It leaves out other SUBTOTAL cells inside its range, so nested totals are not counted twice.

Syntax

SUBTOTAL(function_code, range1, [range2, ...])

Arguments

ArgumentTypeRequiredWhat it does
function_codenumberyesThe aggregate: 1 AVERAGE, 2 COUNT, 3 COUNTA, 4 MAX, 5 MIN, 6 PRODUCT, 7 STDEV, 8 STDEVP, 9 SUM, 10 VAR, 11 VARP. Add 100 to also skip rows hidden by hand.
range1rangeyesThe first range to aggregate, vertical.
range2rangenoMore ranges. You can repeat this argument.

Examples

The examples use this table, starting in A1:

RegionProductUnitsRevenue
NorthLamp10250
SouthChair4600
NorthChair6900
EastLamp375
SouthLamp8200
=SUBTOTAL(9, C2:C6)

Result: 31

Sum of units. Code 9 is SUM.

Run in a Google Sheet (English US locale) on .

=SUBTOTAL(1, C2:C6)

Result: 6.2

Average of units. Code 1 is AVERAGE.

Run in a Google Sheet (English US locale) on .

=SUBTOTAL(4, D2:D6)

Result: 900

Largest revenue. Code 4 is MAX.

Run in a Google Sheet (English US locale) on .

=SUBTOTAL(109, C2:C6)

Result: 31

Sum that also skips rows hidden by hand. With no hidden rows it equals code 9.

Run in a Google Sheet (English US locale) on .

=SUBTOTAL(103, A2:A6)

Result: 5

Count of non-empty cells in the region column. Code 3 is COUNTA.

Run in a Google Sheet (English US locale) on .

Common errors

  • Codes 1 to 11 count rows hidden by hand. Codes 101 to 111 skip them.
  • An unknown code, such as 12, returns #VALUE!.
  • Other SUBTOTAL cells inside the range are ignored. A range with 10, 20 and a SUBTOTAL of both gives 30 with SUBTOTAL, but 60 with SUM.

Compared with Excel

Same function codes as Excel, 1 to 11 and 101 to 111.

Questions and answers

Which code gives a sum: 9 or 109?

Both add. 109 also leaves out rows hidden by hand, so it fits reports where people hide rows.

Sources

  1. SUBTOTAL (Docs Editors Help)

Markdown version · All functions