Function · Math

Excel SUBTOTAL function

The Excel SUBTOTAL function returns a sum, average, count, or other aggregate that ignores other SUBTOTAL cells. Function 9 sums the four utility bills to $446.35. Function 109 also skips rows you hide by hand.

All current Excel versions. Also in Excel for the web and Google Sheets.

Syntax =SUBTOTAL(function_num, ref1, [ref2], ...)

SUBTOTAL
=SUBTOTAL(9,B2:B5)

Arguments

NameRequiredWhat to pass
function_numRequiredWhich aggregate. 9 is SUM, 1 is AVERAGE, 2 is COUNT, 4 is MAX, 5 is MIN. Add 100 to ignore rows hidden by hand: 109 is SUM that skips those rows. Filtered-out rows are skipped by both sets.
ref1RequiredThe first range. Other SUBTOTAL results inside it are ignored, so a grand total does not add the subtotals a second time.
ref2OptionalMore ranges.

Examples

The four utility bills, summed so nested subtotals would not be counted twice

=SUBTOTAL(9,B2:B5)

Result $446.35

=SUBTOTAL(9,B2:B5)B6
Bills
AB
1MonthBill
2Jan143.60
3Feb96.20
4Mar118.45
5Apr88.10
6Subtotal446.35
A picture of the sheet, not a live calculator. 9 means SUM and still includes a row you hide with the Hide command. 109 skips that row. A filter skips the row either way.

SUBTOTAL runs one of eleven aggregates and refuses to count another SUBTOTAL inside its range. Function 9 is SUM. On the four utility bills it returns $446.35, the same number SUM returns, because this range has no nested subtotal. Put a category subtotal in the column and a grand SUBTOTAL of the whole column adds the detail once. SUM would add the detail and then add the subtotal again.

The code also decides what “hidden” means. 9 ignores rows a filter has hidden and still includes a row you hid with the Hide command. 109 ignores both. AVERAGE is 1 or 101, COUNT is 2 or 102, MAX is 4 or 104, MIN is 5 or 105. AGGREGATE is the later function that can also ignore errors. SUBTOTAL cannot. One #N/A in the range makes the subtotal #N/A.