What is the SUBTOTAL function?
=SUBTOTAL(function_num, ref1, [ref2], ...)SUBTOTAL returns a subtotal of a list. Choose the calculation — sum (9), average (1), count (2) and so on — with function_num, and it automatically leaves out other SUBTOTAL results in the range so subtotals are not added twice.
Rows hidden by a filter are always left out, whatever the function_num. Rows you hide yourself are included with 1–11 and left out with 101–111. Check above that the total is 262,900 even though the subtotal cells C5 and C8 are in the range.
Arguments
- function_num
- 1 AVERAGE, 2 COUNT, 3 COUNTA, 4 MAX, 5 MIN, 6 PRODUCT, 7 STDEV, 8 STDEVP, 9 SUM, 10 VAR, 11 VARP. Adding 100 (101–111) does the same calculation but leaves out hidden rows.
- ref1, ref2, ...
- The ranges to subtotal, up to 254.
SUBTOTAL examples
Example 1. Grand total without subtotals
=SUBTOTAL(9, C2:C8)Skips the subtotal cells C5 and C8 and adds only the product sales: 262,900. SUM would include the subtotals and give 525,800.
Example 2. Total only filtered rows
=SUBTOTAL(9, C2:C100)Filter by branch and only the visible rows are added. The result follows every filter change.
Example 3. Leave out hidden rows
=SUBTOTAL(109, C2:C8)Also leaves out rows hidden with Hide Rows.
Common mistakes
Mixing up 9 and 109
With filters only, they are the same. They differ only when rows are hidden by hand.
Horizontal ranges
According to Microsoft, SUBTOTAL is designed for vertical ranges. In a horizontal range, hiding columns does not change the result of 109.
Mixing with SUM subtotals
If subtotals are made with SUM, SUBTOTAL does not recognize them and adds them twice. Make subtotals with SUBTOTAL too.
FAQ
- Insert subtotals automatically?
- The Subtotal command on the Data tab inserts SUBTOTAL formulas for each group automatically.
- I want to skip error values.
- SUBTOTAL returns an error if the range contains one. For calculations that ignore errors, look at the AGGREGATE function.