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.
Syntax =SUBTOTAL(function_num, ref1, [ref2], ...)
=SUBTOTAL(9,B2:B5)Arguments
| Name | Required | What to pass |
|---|---|---|
| function_num | Required | Which 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. |
| ref1 | Required | The first range. Other SUBTOTAL results inside it are ignored, so a grand total does not add the subtotals a second time. |
| ref2 | Optional | More ranges. |
Examples
The four utility bills, summed so nested subtotals would not be counted twice
=SUBTOTAL(9,B2:B5)Result $446.35
| A | B | |
|---|---|---|
| 1 | Month | Bill |
| 2 | Jan | 143.60 |
| 3 | Feb | 96.20 |
| 4 | Mar | 118.45 |
| 5 | Apr | 88.10 |
| 6 | Subtotal | 446.35 |
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.