Math & trig function

SUBTOTAL function

Returns a sum, count, average and more for a list, ignoring rows hidden by a filter and other SUBTOTAL results.

Available in All versions of Excel, Excel for the web, Excel for Mac

In every Excel versionWorks in Google Sheets

Syntax

SUBTOTAL(function_num, ref1, [ref2], ...)
ArgumentWhat it means
function_numRequiredWhich calculation: 1 AVERAGE, 2 COUNT, 3 COUNTA, 4 MAX, 5 MIN, 6 PRODUCT, 7 STDEV, 8 STDEVP, 9 SUM, 10 VAR, 11 VARP. Add 100 (101–111) to also ignore rows you hid manually.
ref1RequiredThe range to calculate on.
ref2, ...OptionalUp to 253 more ranges.

Returns: The chosen statistic for the visible cells of the range.

How SUBTOTAL works

SUBTOTAL is one function that can behave like eleven: SUM, COUNT, AVERAGE, MAX and others, selected by the first argument. What makes it special is visibility. When you filter a table, SUBTOTAL only uses the rows that are still showing, so a total under a filtered list always matches what you see. It is the formula Excel writes in a table's Total Row and in Data > Subtotal.

There are two sets of codes. Codes 1–11 ignore rows hidden by a filter but include rows you hid by hand (right-click > Hide). Codes 101–111 ignore both. In both cases, any cell in the range that itself contains a SUBTOTAL (or AGGREGATE) formula is skipped, so you can put group subtotals inside a list and still get a correct grand total over the whole column without double counting.

SUBTOTAL works on vertical ranges; hiding columns does not affect it. It cannot skip error values — for that, and for more functions such as MEDIAN or LARGE, use AGGREGATE.

Examples

1Total of a list (and of what is left after filtering)

C7fx=SUBTOTAL(9, C2:C6)
ABC
1RepRegionSales
2AnaEast4200
3BenWest3150
4ChloeEast5100
5DevNorth2800
6EmaWest3900
719,150

Result: 19,150

With no filter the result equals SUM. Filter Region to East and the same formula shows 9,300 because only Ana and Chloe are visible.

2Grand total that skips group subtotals

C8fx=SUBTOTAL(9, C2:C7)
ABC
1RegionRepSales
2EastAna4200
3EastChloe5100
4East total9300
5WestBen3150
6WestEma3900
7West total7050
816,350

Result: 16,350

C4 and C7 hold =SUBTOTAL(9,C2:C3) and =SUBTOTAL(9,C5:C6). The grand total ignores them, so it is 16,350, not 32,700. A plain SUM over C2:C7 would double count.

3Ignore a row hidden by hand

B7fx=SUBTOTAL(109, B2:B6)
AB
1ProductStock
2Bolts340
3Nuts515
4Washers (old)120
5Screws410
6Rivets95
71360

Result: 1360

Assume row 4 (discontinued washers) was hidden with right-click > Hide. Code 109 skips it and returns 1,360; code 9 would still include it and return 1,480.

4Count visible rows in a filtered list

E1fx=SUBTOTAL(103, A2:A6)
ABCDE
1RepRegionSales5
2AnaEast4200
3BenWest3150
4ChloeEast5100
5DevNorth2800
6EmaWest3900

Result: 5

103 is COUNTA for visible rows. Unfiltered it returns 5; filter to West and it returns 2, which is handy for a "showing X of 5 reps" label.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 18 KB

Common errors and how to fix them

ErrorWhy it happensFix
#VALUE!function_num is not 1–11 or 101–111.Use one of the listed codes; for other functions use AGGREGATE.
#DIV/0!Average (1/101) of a range where every row is filtered out or empty.Wrap in IFERROR or check the filter.
Error in the dataA cell in the range holds an error, which SUBTOTAL passes through.Use =AGGREGATE(9, 7, range) to ignore errors and hidden rows.

Tips

  • Put SUBTOTAL formulas above the header row so they stay visible while the filter changes the list.
  • In an Excel Table, tick Total Row and choose Sum/Average from the drop-down; Excel writes SUBTOTAL with 109/101 for you.
  • Codes 1–11 and 101–111 behave the same on filtered rows; the difference is only rows hidden manually or by grouping.
  • Combine with a helper column =SUBTOTAL(103, A2) to flag visible rows, then use SUMIFS on that flag for conditional visible-only totals.

Questions

What is the difference between 9 and 109?

Both ignore rows hidden by a filter. 109 also ignores rows hidden manually or collapsed by grouping; 9 includes them.

SUBTOTAL or AGGREGATE?

AGGREGATE can also ignore error values and offers 19 functions including MEDIAN, LARGE and PERCENTILE. SUBTOTAL is simpler and works in every Excel version.

Google Sheets

SUBTOTAL also exists in Google Sheets and works the same way for everyday use, so the examples above work there too.

SUBTOTAL in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
DutchSUBTOTAAL
FinnishVÄLISUMMA
FrenchSOUS.TOTAL
GermanTEILERGEBNIS
HungarianRÉSZÖSSZEG
ItalianSUBTOTALE
Norwegian (Bokmål)DELSUM
PolishSUMY.CZĘŚCIOWE
RussianПРОМЕЖУТОЧНЫЕ.ИТОГИ
SpanishSUBTOTALES
SwedishDELSUMMA
TurkishALTTOPLAM

Recipes that use SUBTOTAL