Subtotal Formula in Excel: SUM, SUBTOTAL, and More

Formulas & functions · Updated November 23, 2026

The direct answer: there is no single “subtotal formula” in Excel. To add a simple subtotal, use =SUM() on the range you want to total. To create automatic subtotals that ignore hidden rows or nested totals, use the SUBTOTAL() function with a function number like 9 (SUM) or 109 (SUM ignoring hidden rows).

What the SUBTOTAL Function Actually Does

SUBTOTAL is a versatile function that performs 11 different operations (SUM, AVERAGE, COUNT, MAX, MIN, etc.) but with two key advantages over the regular functions. First, it automatically ignores other SUBTOTAL results inside its range, so you can safely add grand totals that include subtotals without double-counting. Second, when you filter a table or hide rows manually, SUBTOTAL can be set to only count visible cells.

The syntax is =SUBTOTAL(function_num, ref1, [ref2], ...). The first argument is a number from 1 to 11 (includes hidden values) or 101 to 111 (ignores manually hidden rows). For summing, use 9 (includes hidden) or 109 (excludes hidden). For averaging, use 1 or 101, and so on.

How to Insert a Subtotal in a Data List

If your data is a simple list with column headers, Excel has a built-in command that inserts subtotal rows automatically. Select any cell in your data, then go to Data > Outline > Subtotal. In the dialog box:

  • Choose the column to group by (e.g., “Region”) in “At each change in”.
  • Pick the function (usually Sum) and the column to total (e.g., “Sales”).
  • Check “Replace current subtotals” and “Summary below data” as needed.

Excel inserts a row after each group with =SUBTOTAL(9, ...) and an outline on the left to collapse groups. This is the fastest way to add subtotals to a sorted list. Note that the data must be sorted by the grouping column first, or the subtotals will appear at random points.

SUBTOTAL vs. SUM: Which One to Use

Use =SUM() when you always want the total of every cell in the range, even if rows are hidden or filtered. Use =SUBTOTAL(109, ...) when you want the total to change as the user filters or hides rows. A common mistake is using =SUM() for a grand total that includes subtotal rows—this double-counts. Instead, use =SUBTOTAL(9, ...) over the entire range, and it will skip the nested subtotals automatically.

Example: Suppose you have sales in B2:B10, with subtotals at B5 and B8. A grand total in B12 with =SUM(B2:B10) would add the subtotals twice. Using =SUBTOTAL(9, B2:B10) gives the correct grand total because it ignores the subtotal formulas inside the range.

Using SUBTOTAL with Filters and Hidden Rows

When you apply a filter to a table or range, =SUBTOTAL(9, ...) automatically ignores rows hidden by the filter, because filtered rows are considered hidden. However, if you manually right-click and hide rows (not via filter), function number 9 still includes them. To exclude manually hidden rows, use function number 109. This is a frequent source of confusion, so test both with a small dataset to see the difference.

For example, with values 10, 20, 30 in A1:A3, if you hide row 2 manually, =SUBTOTAL(9, A1:A3) returns 60, while =SUBTOTAL(109, A1:A3) returns 40. If you instead filter to show only rows 1 and 3, both 9 and 109 return 40, because filtering counts as hiding.

Common Errors and How to Fix Them

  • #VALUE! error: Occurs if you use a function number outside 1-11 or 101-111, or if ref1 contains a non-numeric value that the chosen function cannot handle (e.g., AVERAGE with text).
  • Double-counting in grand totals: Fix by using SUBTOTAL for both subtotals and the grand total, not SUM over the whole range.
  • Subtotals not updating when filtering: Make sure you used function number 101-111 (like 109) instead of 1-11.
  • Data > Subtotal greyed out: The command only works on a normal range, not an Excel Table. Convert the table back to a range (Table Design > Tools > Convert to Range) or use the SUBTOTAL function manually.

Alternatives to SUBTOTAL

For more complex grouping, consider the AGGREGATE function, which offers even more options (like ignoring error values) with function numbers 1-19 and options 0-7. For dynamic arrays in Excel 365, you can use =GROUPBY() (Excel 365 only) to create a summary table with subtotals per group automatically. PivotTables also produce subtotals and grand totals with a few clicks and are often the best choice for large datasets or when you need to rearrange the summary layout.

If you just need a quick total for a selected range, select the cells and look at the status bar at the bottom of Excel—it shows the sum, average, and count without writing any formula.

Frequently Asked Questions

How do I add a subtotal in Excel on Mac? The Data > Subtotal command works the same on Mac. The keyboard shortcut to open the Subtotal dialog is not standard, but you can use the menu. The SUBTOTAL function syntax is identical on Mac.

Why does my SUBTOTAL formula show 0? Check that the function number is correct (9 for sum) and that the range contains numeric values. If the range includes text or empty cells, SUM ignores them, but if you used AVERAGE and the range has text, you may get an error. Also ensure the cells are not formatted as text.

Can I use SUBTOTAL inside a SUM formula? Yes, but it is usually unnecessary. For example, =SUM(SUBTOTAL(9, A1:A5), SUBTOTAL(9, C1:C5)) adds two subtotals together. Just be careful not to double-count if the ranges overlap.

Does SUBTOTAL work in Google Sheets? Yes, Google Sheets has the same SUBTOTAL function with identical syntax. The Data > Subtotal menu command is not available in Sheets, so you must type the formula manually.

What is the difference between SUBTOTAL and AGGREGATE? AGGREGATE can ignore error values and hidden rows, and offers more functions (like LARGE, SMALL, PERCENTILE). However, it has a more complex syntax with an options argument. For basic subtotals, SUBTOTAL is simpler.

How do I remove subtotals I added with Data > Subtotal? Go to Data > Subtotal and click “Remove All”. This deletes the subtotal rows and the outline, but keeps your original data intact.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.