Skip to content

SUBTOTAL

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], …)

Math and trigonometry

All versions of Excel.

Try it

Edit the formula in B7 or change the inputs. The sheet recalculates as you type.

B7
A
B
1
Item
Cost
2
Paint
120
3
Brushes
45
4
Tape
30
5
Sheeting
65
6
Rollers
90
7
Total
350

B7 =SUBTOTAL(9,B2:B6) returns 350

How SUBTOTAL works

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.

Choosing the calculation

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.

Nested subtotals

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.

Filtered rows and hidden rows

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:

  • Codes 1-11 still include manually hidden rows.
  • Codes 101-111 leave them out.

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.

SUBTOTAL compared with SUM and AGGREGATE

  • SUM adds everything in the range, hidden or not.
  • SUBTOTAL has 11 calculations and the nested-subtotal rule. It returns an error if any cell in the range is an error.
  • AGGREGATE adds more calculations (median, large, small, percentile) and can also skip errors.

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.

Arguments

function_num
Which calculation to run. 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.
ref1
The range to calculate over, such as B2:B20.
ref2, …optional
More ranges, up to 254.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

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
A
B
1
Item
Cost
2
Paint
120
3
Brushes
45
4
Tape
30
5
Sheeting
65
6
Rollers
90
7
Total
350

B7 =SUBTOTAL(9,B2:B6) returns 350

Example 2

A grand total that skips the group subtotals

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
A
B
1
Item
Cost
2
Paint
100
3
Brushes
200
4
Tape
150
5
Materials
450
6
Labour day 1
80
7
Labour day 2
120
8
Labour day 3
60
9
10
Grand total
710

B10 =SUBTOTAL(9,B2:B8) returns 710

Example 3

Other calculations: largest value

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
A
B
1
Item
Cost
2
Paint
120
3
Brushes
45
4
Tape
30
5
Sheeting
65
6
Rollers
90
7
Largest
120

B7 =SUBTOTAL(4,B2:B6) returns 120

Example 4

Count the numeric rows, ignoring a subtotal row

B8 =SUBTOTAL(2,B2:B6)

Result: 4

Code 2 is COUNT. B4 is a SUBTOTAL, so only the four detail rows are counted.

B8
A
B
1
Item
Cost
2
Paint
100
3
Brushes
200
4
Materials
300
5
Labour day 1
80
6
Labour day 2
120
7
8
Detail rows
4
9

B8 =SUBTOTAL(2,B2:B6) returns 4

Your turn.

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.

B10
A
B
C
1
Item
Cost
2
Cabling
120
3
Switches
340
4
Patch panels
95
5
Network total
555
6
Laptops
210
7
Docks
85
8
Monitors
150
9
Desk total
445
10
Project total
11
Largest single item

Common mistakes

  • Codes 1-11 still include rows you hid by hand. If users hide rows, use 101-111 so hidden rows are left out as well as filtered ones.
  • SUBTOTAL ignores only other SUBTOTAL and AGGREGATE formulas. A subtotal row typed with plain SUM is counted again in the grand total.
  • If any cell in the range holds an error, SUBTOTAL returns that error. Use AGGREGATE with option 6 to skip errors.
  • A hidden column is not skipped. SUBTOTAL only responds to hidden rows, so it can't total a horizontal range by what is visible.
  • Putting the SUBTOTAL inside its own range is a circular reference. Keep the grand total in a row or column outside the data.

Questions people ask

What is the difference between SUBTOTAL 9 and 109?

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.

Why does SUBTOTAL ignore my other SUM formulas?

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.

How do I total only the filtered rows?

Put `=SUBTOTAL(109, B2:B100)` under the filtered column. The result changes when you change the filter, while SUM would keep adding everything.

What function numbers does SUBTOTAL accept?

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.

Does SUBTOTAL work on hidden columns?

No. It only reacts to hidden or filtered rows. Hiding a column doesn't change the result.

Learn SUBTOTAL in a real sheet.

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.