Excel function

SUBTOTALfunction

Returns a subtotal of a list, leaving out other SUBTOTAL results in the range.

All versions
=SUBTOTAL(9, C2:C8)
A
B
C
D
1
2
3
4
5
6
7
8

Drag the corner dots to change ref1. Set the hidden row to "Row 3 (Bread) hidden" and switch function_num between 9 and 109. Only 109 leaves out the Bread sales. Try breaking it on purpose to see the errors.

Result

262900

function_num

Which calculation to do. 1–11 include hidden rows; 101–111 leave them out. Rows hidden by a filter are left out by both.

ref1
C2:C8Drag a corner dot up/down to resize, sideways to change the column

The range to subtotal. It may contain the subtotal cells (C5, C8).

Hidden row

Pretends a row is hidden as in Excel's Hide Rows. It is not a formula argument.

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

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

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

  3. Example 3. Leave out hidden rows

    =SUBTOTAL(109, C2:C8)

    Also leaves out rows hidden with Hide Rows.

Common mistakes

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.