The SUBTOTAL function in Excel is used to perform calculations—such as summing, averaging, or counting—on a range of cells while ignoring rows that have been hidden by a filter. Unlike the standard SUM function, which calculates every cell in a range regardless of visibility, SUBTOTAL dynamically updates its result based on the data currently visible on your screen. This makes it the essential tool for creating interactive reports and dashboards where the user needs to see totals change as they apply different filters to a dataset.
What You'll Need
- A computer with Microsoft Excel installed (works across Windows, macOS, and Web versions).
- A dataset organized in a tabular format (rows and columns) with headers.
- Basic knowledge of how to select cell ranges in Excel.
Step-by-Step
- Prepare Your Data: Ensure your data is organized in a clean table. For example, if you are tracking sales, you should have columns for "Date," "Product," "Region," and "Amount." Make sure there are no completely empty rows or columns within the data block, as this can interfere with how Excel recognizes the range.
- Apply a Filter: To see the true power of the SUBTOTAL function, you must first enable filtering. Select your header row, go to the Data tab on the Ribbon, and click the Filter button. You will now see drop-down arrows on each column header.
- Select the Target Cell: Click on the cell where you want the total to appear. This is typically located either directly above the data range (in a frozen top row) or directly below the last row of data.
- Enter the Function Syntax: Type
=SUBTOTAL(into the cell. The function requires two arguments: the function_num (which tells Excel what math to do) and the ref1 (the range of cells to calculate). - Choose the Calculation Type: After the opening parenthesis, enter the number corresponding to the operation you want. For a sum, use
9or109. For an average, use1or101. For a count, use2or102. For example, to sum the values, your formula should start as=SUBTOTAL(9,. - Define the Range: After the comma, click and drag your mouse over the cells you want to calculate (e.g., the "Amount" column from cell D2 to D100). Your formula should now look like
=SUBTOTAL(9, D2:D100). - Complete the Formula: Press Enter. The cell will now display the total for that range.
- Test the Dynamic Result: Use the filter arrows in your header row to filter by a specific region or product. Notice that the result of the SUBTOTAL formula changes instantly to reflect only the rows that remain visible, whereas a standard SUM formula would continue to show the total of all rows, including the hidden ones.
Common Mistakes
One of the most frequent errors is using the SUBTOTAL function on a range that already contains other SUBTOTAL formulas. Because SUBTOTAL is designed to ignore other SUBTOTAL results within its range, it prevents "double counting." However, if you manually add a SUM formula in the middle of your range and then use SUBTOTAL at the bottom, the SUM result will be included in the final total, leading to an inflated and incorrect number.
Another common mistake is confusing the function numbers. Users often forget whether 9 or 109 is the correct code for summing. While both perform a sum, they handle manually hidden rows differently. If you use 9, Excel ignores rows hidden by a Filter but includes rows that were hidden manually (by right-clicking a row and selecting "Hide"). If you use 109, Excel ignores both filtered-out rows and manually hidden rows. Choosing the wrong one can lead to discrepancies in your financial reporting if you frequently hide rows manually.
Lastly, some users try to use SUBTOTAL across multiple non-contiguous ranges without properly separating them. While SUBTOTAL allows for multiple ranges (e.g., =SUBTOTAL(9, A1:A10, C1:C10)), adding too many disparate ranges can make the formula difficult to audit and prone to errors if rows are inserted or deleted later.
Tips
To make your reports more professional, place your SUBTOTAL formula at the top of your sheet rather than the bottom. When dealing with thousands of rows of data, scrolling to the bottom to see the total is inefficient. By placing the formula in row 1 and using View > Freeze Panes > Freeze Top Row, your filtered totals will always be visible regardless of how far down you scroll.
If you are using an Excel Table (created by pressing Ctrl + T), you don't even need to type the formula. You can simply go to the Table Design tab and check the box for Total Row. Excel will automatically insert a SUBTOTAL function at the bottom of the table. You can then click the drop-down menu in that total cell to switch between Sum, Average, Count, and other operations without typing a single line of code.
For advanced users, combining SUBTOTAL with the AGGREGATE function can provide even more control. While SUBTOTAL ignores filtered rows, AGGREGATE can ignore filtered rows, manually hidden rows, and error values (like #N/A or #DIV/0!), ensuring your total doesn't break just because one cell in your data has an error.
Troubleshooting
The total is not changing when I filter: First, check if you used the SUM function instead of the SUBTOTAL function. SUM is static; SUBTOTAL is dynamic. Second, ensure that you are using a Filter (Data > Filter) and not just manually hiding rows if you used function code 9. If you manually hid the rows, you must change the function code to 109.
The formula returns a #VALUE! error: This usually happens if the range you selected contains text or non-numeric data that the specific function cannot handle. Check your range for cells that look like numbers but are actually formatted as text. You can fix this by selecting the column, going to the Data tab, selecting Text to Columns, and clicking Finish to force Excel to re-evaluate the data types.
The result seems too high: Check for "nested" totals. If you have several SUBTOTAL formulas throughout your column and one master SUBTOTAL at the bottom, the master one should ignore the others. However, if you used a standard SUM for those intermediate totals, the master SUBTOTAL will count them, effectively doubling your numbers. Replace all intermediate SUMs with SUBTOTALs.
Frequently Asked Questions
What is the difference between function 9 and 109?
Function 9 ignores rows hidden by a filter but includes rows hidden manually. Function 109 ignores both filtered-out rows and rows hidden manually via the right-click menu.
Can SUBTOTAL be used for counting text?
Yes. Use function code 2 or 102 (COUNTA). This will count the number of non-empty cells in the visible range, regardless of whether they contain numbers or text.
Does SUBTOTAL work with Pivot Tables?
No, SUBTOTAL is designed for raw data ranges and Excel Tables. Pivot Tables have their own built-in aggregation methods (Sum, Count, Average) that automatically handle filtering through Slicers or Report Filters.
Can I use SUBTOTAL to find the maximum or minimum value?
Yes. Use function code 4 (MAX) or 5 (MIN) to find the highest or lowest value among the currently visible rows in your filtered list.
Will SUBTOTAL slow down my workbook?
In most cases, no. However, if you have tens of thousands of SUBTOTAL formulas calculating across massive ranges in a single sheet, you may notice a slight lag during filtering as Excel recalculates the visible range.