# SUBTOTAL in Google Sheets: syntax and examples

URL: https://goingstellar.ro/reference/sheets/subtotal/

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

- `function_code` (number, required: yes): The 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.
- `range1` (range, required: yes): The first range to aggregate, vertical.
- `range2` (range, required: no): More ranges. You can repeat this argument.

## Examples

The examples use this table, starting in A1:

| Region | Product | Units | Revenue |
|---|---|---|---|
| North | Lamp | 10 | 250 |
| South | Chair | 4 | 600 |
| North | Chair | 6 | 900 |
| East | Lamp | 3 | 75 |
| South | Lamp | 8 | 200 |

```
=SUBTOTAL(9, C2:C6)
```

Result: `31`

Sum of units. Code 9 is SUM. Run in a Google Sheet (English US locale) on 2026-11-29.

```
=SUBTOTAL(1, C2:C6)
```

Result: `6.2`

Average of units. Code 1 is AVERAGE. Run in a Google Sheet (English US locale) on 2026-11-29.

```
=SUBTOTAL(4, D2:D6)
```

Result: `900`

Largest revenue. Code 4 is MAX. Run in a Google Sheet (English US locale) on 2026-11-29.

```
=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 2026-11-29.

```
=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 2026-11-29.

## 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.

## Related functions

- [SUM](https://goingstellar.ro/reference/sheets/sum/)
- [SUMIF](https://goingstellar.ro/reference/sheets/sumif/)
- [SUMIFS](https://goingstellar.ro/reference/sheets/sumifs/)
- [COUNTA](https://goingstellar.ro/reference/sheets/counta/)
- [AVERAGE](https://goingstellar.ro/reference/sheets/average/)
- [FILTER](https://goingstellar.ro/reference/sheets/filter/)

## Sources

- [SUBTOTAL (Docs Editors Help)](https://support.google.com/docs/answer/3093649)

