The SUBTOTAL function in Excel returns a calculated subtotal for a range of cells, and its formula is =SUBTOTAL(function_num, ref1, [ref2], ...). For example, =SUBTOTAL(9, A2:A10) sums the values in A2:A10, while ignoring any rows hidden by a filter.
What the SUBTOTAL Function Does
SUBTOTAL is a versatile aggregation function that can perform 11 different operations—sum, average, count, max, min, and more—depending on the first argument you supply. Its key advantage over regular functions like =SUM() is its behavior with hidden rows. When you filter a table or manually hide rows, SUBTOTAL recalculates to include only the visible cells, which is essential for dashboards and reports.
Function Numbers and What They Mean
The first argument, function_num, is a number from 1 to 11 (or 101 to 111) that tells Excel which operation to perform. The numbers 1–11 include hidden rows (but not filtered-out rows), while 101–111 ignore manually hidden rows as well as filtered rows. Here are the most common ones:
- 1 or 101 – AVERAGE
- 2 or 102 – COUNT (numeric cells)
- 3 or 103 – COUNTA (non-empty cells)
- 4 or 104 – MAX
- 5 or 105 – MIN
- 6 or 106 – PRODUCT
- 9 or 109 – SUM
For most users, 9 (sum) and 109 (sum ignoring hidden rows) are the go-to choices. The difference matters when you manually hide rows (right-click > Hide) versus using a filter. With a filter, both 9 and 109 ignore hidden rows; with manual hiding, only 109 ignores them.
How to Write a SUBTOTAL Formula
Assume you have sales figures in cells B2:B20 and you want a filtered sum. In cell B21, type:
=SUBTOTAL(9, B2:B20)
This returns the sum of all visible cells when a filter is active. If you also want to ignore manually hidden rows, use =SUBTOTAL(109, B2:B20) instead. The result updates automatically as you filter or hide rows. For example, if B2:B20 contains 10, 20, 30, and you filter out the 20, the formula returns 40.
Using SUBTOTAL with Filtered Lists and Tables
SUBTOTAL is particularly useful in Excel Tables (Insert > Table). When you add a Total Row to a table, Excel automatically inserts a SUBTOTAL formula in the last column, defaulting to 109. This means the total row always reflects only visible rows after filtering. To change the operation, click the dropdown arrow in the total row cell and select e.g., Average or Count.
For a non-table range, you can still apply a filter (Data > Filter) and then use SUBTOTAL in a cell above or below the data. Note that SUBTOTAL ignores other SUBTOTAL formulas in the same range, so nested subtotals won’t double-count.
Common Mistakes and How to Avoid Them
One frequent error is using =SUM() instead of =SUBTOTAL(9, ...) when you need filtered totals—SUM always includes hidden rows. Another is forgetting that the function_num 1–11 and 101–111 behave differently with manual hiding. Also, SUBTOTAL only works on vertical ranges; if your data is horizontal (e.g., A1:F1), it still works, but the 101–111 numbers ignore hidden columns only if you hide columns, not rows. Finally, ensure you reference the correct range—if you include the total row itself in the range, you’ll get a circular reference.
SUBTOTAL vs. AGGREGATE vs. SUM
SUM is the simplest but ignores nothing. SUBTOTAL adds the ability to skip hidden rows and is easier to read than AGGREGATE. AGGREGATE (available in Excel 2010 and later) is more powerful: it can ignore error values and offers more functions (like LARGE or MEDIAN). For example, =AGGREGATE(9, 7, B2:B20) sums while ignoring hidden rows and error values. However, AGGREGATE requires two extra arguments and is less intuitive. For most filtering needs, SUBTOTAL is the right balance of simplicity and functionality.
Keyboard Shortcuts and Mac Differences
To quickly insert a SUBTOTAL total row in a table, click anywhere in the table and press Ctrl+Shift+T (Windows) or Cmd+Shift+T (Mac). On Mac, the function works identically, but the shortcut may differ in older versions. For a quick sum of visible cells without a formula, select the range and look at the status bar—it shows the sum, but that is not a formula you can reuse.
Frequently Asked Questions
Does SUBTOTAL work with filtered data in Google Sheets?
Yes, Google Sheets has a SUBTOTAL function with the same syntax and function numbers. The behavior with filters is identical, but manual hiding of rows is handled differently—in Sheets, 1–11 and 101–111 both ignore manually hidden rows, which differs from Excel.
Why does my SUBTOTAL return 0?
Check that your range contains numbers, not text. Also, if you used function_num 2 (COUNT) it counts numbers only, not text. If you filtered out all rows, the result is 0. Verify that the range doesn’t include the formula cell itself.
Can I use SUBTOTAL inside another SUBTOTAL?
Yes, but Excel ignores nested SUBTOTALs to avoid double counting. For example, if you have a SUBTOTAL in B21 and another in B22 that references B21, the outer one will ignore the inner one’s result, which may be unexpected.
What is the difference between 9 and 109 in SUBTOTAL?
9 includes manually hidden rows, while 109 excludes them. Both ignore rows hidden by a filter. Use 109 if you hide rows manually and want the total to update accordingly.
Does SUBTOTAL work on multiple ranges?
Yes, you can add up to 254 range arguments, like =SUBTOTAL(9, A2:A10, C2:C10). It sums both ranges, but note that if you filter one column, the other range is not filtered automatically—you need to filter the whole table.
Is there a way to subtotal by category?
Yes, use the Subtotal command under Data > Subtotal (not the function). It automatically inserts SUBTOTAL formulas and group outlines based on a category column. This is different from the SUBTOTAL function, which only calculates one aggregate for a single range.
Related
- Butterfly Life Cycle for 3rd Grade (Simple Guide)
- B is for Butterfly Worksheet: Free Printables & Uses
- How to Calculate Battery Size for a Solar System
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Business Central Functions: Core Features ExplainedBusiness Central functions are the built-in capabilities—financial management, supply chain, sales, purchasing, project management, and reporting—that let small…
- Excel Conditional Formatting: Use a Formula for RulesYes. In Excel, conditional formatting can use a formula that evaluates to TRUE or FALSE to decide which…
- SQL String Replace Function: Syntax and ExamplesUse SQL’s REPLACE function to find every occurrence of one text value inside another and substitute different text:…
- C String Functions: How They Work and When to Use ThemC string functions are standard library routines declared in <string.h> for measuring, copying, comparing, joining, and searching strings.…
- COUNTIF Negative Numbers in Excel: Formula and TipsIf your values are in A2:A20, count the negative numbers with =COUNTIF(A2:A20,"<0"). COUNTIF returns the number of cells…
- Count if Formula in Excel: Step-by-Step GuideThe COUNTIF formula in Excel counts cells that meet a single condition, and its syntax is =COUNTIF(range, criteria).…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.