Example 1
A plain total
B7 =SUBTOTAL(9,B2:B6)
Result: 350
With no subtotals or filters involved, code 9 gives the same answer as SUM.
B7 =SUBTOTAL(9,B2:B6) returns 350
Returns a sum, average, count, maximum and more for a list, skipping other SUBTOTALs in the range and rows hidden by a filter.
Syntax
=SUBTOTAL(, , [ref2], …)
All versions of Excel.
Try it
Edit the formula in B7 or change the inputs. The sheet recalculates as you type.
B7 =SUBTOTAL(9,B2:B6) returns 350
SUBTOTAL runs a calculation over a list and, unlike SUM, knows about the rest of the sheet. It leaves out any other SUBTOTAL (or AGGREGATE) formula inside its range, and it leaves out rows hidden by a filter. That makes it the right function for lists that carry their own subtotals and for filtered tables.
The first argument is a code that picks the calculation. The ones you will use most are 9 for SUM, 1 for AVERAGE, 2 for COUNT (numbers only), 3 for COUNTA (any non-blank cell), 4 for MAX and 5 for MIN. Codes 7 to 11 are standard deviation and variance.
Suppose rows 2 to 4 are one group with a subtotal in row 5, and rows 6 to 8 are a second group. A grand total of =SUBTOTAL(9, B2:B8) adds the six detail rows and skips the subtotal in B5. =SUM(B2:B8) would count the group total twice. This is the same reason Excel's Data > Subtotal command writes SUBTOTAL formulas.
When you filter a table, SUBTOTAL leaves out the rows the filter hides, in both families of codes. The two families differ for rows you hide by hand with Hide Row:
So =SUBTOTAL(109, B2:B20) totals only the rows you can see, whichever way they were hidden. Pick 109 over 9 when users hide rows by hand. SUBTOTAL only looks at hidden rows, not hidden columns, so a hidden column is still counted in a horizontal range.
Always keep the SUBTOTAL outside its own range. A total placed inside the range it totals is a circular reference.
Note that the Rangewise sheet has no filters or hidden rows, so the practice examples on this page use the nested-subtotal behaviour only.
1 AVERAGE, 2 COUNT, 3 COUNTA, 4 MAX, 5 MIN, 6 PRODUCT, 7 STDEV.S, 8 STDEV.P, 9 SUM, 10 VAR.S, 11 VAR.P. Add 100 (101 to 111) to also leave out rows you hid by hand.B2:B20.Each example is a live sheet. Change the inputs or the formula and watch the result.
Example 1
B7 =SUBTOTAL(9,B2:B6)
Result: 350
With no subtotals or filters involved, code 9 gives the same answer as SUM.
B7 =SUBTOTAL(9,B2:B6) returns 350
Example 2
B10 =SUBTOTAL(9,B2:B8)
Result: 710
B5 is itself a SUBTOTAL, so the grand total ignores it: 450 + 80 + 120 + 60. SUM(B2:B8) would return 1,160 by counting the materials twice.
B10 =SUBTOTAL(9,B2:B8) returns 710
Example 3
B7 =SUBTOTAL(4,B2:B6)
Result: 120
Code 4 is MAX. Swap the code for 5 to get the smallest, or 1 for the average.
B7 =SUBTOTAL(4,B2:B6) returns 120
Example 4
B8 =SUBTOTAL(2,B2:B6)
Result: 4
Code 2 is COUNT. B4 is a SUBTOTAL, so only the four detail rows are counted.
B8 =SUBTOTAL(2,B2:B6) returns 4
Solve it in the sheet. Every answer is checked, with hints if you need them.
Task 1 of 2
In B10, return the project total of the six cost lines, without counting the two subtotal rows.
Answer in B10.
AGGREGATE with option 6 to skip errors.Both add the visible rows of a filtered list and skip other subtotals. Code 9 still includes rows hidden by hand with Hide Row. Code 109 leaves them out too.
It doesn't. It ignores other SUBTOTAL and AGGREGATE formulas inside its range. A subtotal written with SUM is treated as ordinary data and counted again.
Put `=SUBTOTAL(109, B2:B100)` under the filtered column. The result changes when you change the filter, while SUM would keep adding everything.
1 to 11 and 101 to 111: average, count, counta, max, min, product, stdev.s, stdev.p, sum, var.s and var.p. Use AGGREGATE for median, large, small and percentiles.
No. It only reacts to hidden or filtered rows. Hiding a column doesn't change the result.
SUBTOTAL is taught in Data analysis with formulas, lesson 18: SUBTOTAL and nested totals. Each lesson is a short, graded exercise like the one above.