The Excel SUBTOTAL function allows you 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 or AVERAGE functions, which calculate every cell in a range regardless of visibility, SUBTOTAL dynamically updates its result based on what is currently visible on your screen. This makes it the essential tool for creating reports where you need a "Total" row that changes as you filter your data.
What You'll Need
- A computer with Microsoft Excel installed (works across Windows, macOS, and web versions).
- A dataset containing numeric values.
- A dataset that is formatted as a Table (Ctrl+T) or has Filter arrows enabled in the header row.
Step-by-Step
- Prepare Your Data: Ensure your data is organized in columns with clear headers. For the SUBTOTAL function to be most effective, your data should be in a continuous block without entirely empty rows or columns.
- Enable Filters: Select your header row, go to the Data tab, and click the Filter button. You will now see drop-down arrows on each header. This is critical because SUBTOTAL is designed specifically to interact with these filters.
- Select the Result Cell: Click on the cell where you want the total to appear. This is typically located directly below your last row of data.
- Enter the Function Start: Type
=SUBTOTAL(into the cell. - Choose Your Function Number: Immediately after the opening parenthesis, enter the specific number that corresponds to the calculation you want. For example, if you want to sum the values, type
9or109. If you want an average, type1or101. (Refer to the function number list below for a full breakdown). - Add the Comma: Type a comma after the function number to separate the instruction from the data range.
- Define the Range: Use your mouse to click and drag over the cells you want to calculate, or type the range manually (e.g.,
B2:B50). - Close and Execute: Type the closing parenthesis
)and press Enter. Your formula should look something like=SUBTOTAL(9, B2:B50). - Test the Filter: Use the filter arrows in your header to hide certain categories or values. Observe the SUBTOTAL cell; the number should automatically change to reflect only the visible rows.
The Function Number Reference Table
The first argument of the SUBTOTAL function is the function_num. There are two sets of numbers you can use: 1-11 and 101-111. The difference lies in how they handle rows that are manually hidden (right-click > Hide) versus rows hidden by a filter.
Set 1: Includes Manually Hidden Rows (Only ignores Filtered rows)
1 = AVERAGE
2 = COUNT (counts numbers)
3 = COUNTA (counts non-empty cells)
4 = MAX
5 = MIN
6 = PRODUCT
7 = STDEV.S
8 = STDEV.P
9 = SUM
10 = VAR.S
11 = VAR.P
Set 2: Ignores ALL Hidden Rows (Filtered and Manually Hidden)
101 = AVERAGE
102 = COUNT
103 = COUNTA
104 = MAX
105 = MIN
106 = PRODUCT
107 = STDEV.S
108 = STDEV.P
109 = SUM
110 = VAR.S
111 = VAR.P
Common Mistakes
Including the Subtotal Cell in the Range: One of the most frequent errors is creating a circular reference. This happens when you place your SUBTOTAL formula at the bottom of a column and include the cell containing the formula in the range (e.g., putting the formula in cell B51 and setting the range as B2:B51). This will cause an error or an incorrect calculation. Always ensure the range ends exactly one cell above the formula.
Confusing COUNT and COUNTA: Users often use function number 2 (COUNT) when they are trying to count text entries. Function 2 only counts cells containing numbers. If you are counting names, IDs, or categories, you must use function number 3 (COUNTA), which counts any cell that is not empty.
Using Standard SUM instead of SUBTOTAL: Many users apply a standard =SUM() formula at the bottom of a list and are confused when the total doesn't change after they filter the data. Standard functions always calculate the entire range, regardless of whether the rows are visible or hidden.
Applying SUBTOTAL to a Range with Other SUBTOTALs: If you have multiple subtotal rows within a single large dataset, you might worry about double-counting. Fortunately, the SUBTOTAL function is designed to ignore other SUBTOTAL formulas that occur within the range it is calculating. However, if you use standard SUM formulas for your sub-categories, the final grand total will be doubled.
Tips
Convert to an Excel Table: Instead of manually writing the formula, select your data and press Ctrl + T to turn it into an official Table. Once it is a table, go to the Table Design tab and check the box for Total Row. Excel will automatically insert a SUBTOTAL function at the bottom, and you can change the calculation (Sum, Average, etc.) via a simple drop-down menu without typing any code.
Use the 100-series for Manual Hiding: If your workflow involves right-clicking rows to hide them for presentation purposes (rather than using the filter arrows), always use function numbers 101-111. This ensures that your totals only reflect what the audience can actually see on the screen.
Combining with AGGREGATE: For more advanced users, the AGGREGATE function is a powerful successor to SUBTOTAL. While SUBTOTAL can ignore hidden rows, AGGREGATE can also ignore error values (like #DIV/0!) in your data, preventing your total from breaking just because one cell has an error.
Troubleshooting
The result is not changing when I filter: First, check if you used the SUM function instead of SUBTOTAL. If you are using SUBTOTAL, ensure that you are using a Filter (Data > Filter) and not just manually hiding rows. If you are manually hiding rows, you must use the 100-series function numbers (e.g., 109 instead of 9).
The formula returns #VALUE!: This usually happens if the function number you entered is invalid. Ensure the first number in your formula is between 1-11 or 101-111. If you enter a number like 12 or 112, Excel will not recognize the command and will return a value error.
The count is lower than expected: Check if you are using function number 2 (COUNT) on a column that contains text. If your "numbers" are actually formatted as text (indicated by a small green triangle in the corner of the cell), the COUNT function will ignore them. Convert the cells to Number format or use function number 3 (COUNTA).
Circular Reference Warning: If Excel warns you of a circular reference, check your range. You have likely included the cell where the formula lives inside the range of the formula. Adjust the range to end at the row immediately above the total.
Frequently Asked Questions
Does SUBTOTAL work with hidden rows?
Yes, but it depends on the function number. Numbers 1-11 ignore rows hidden by a filter but include rows hidden manually. Numbers 101-111 ignore both filtered and manually hidden rows.
Can I use SUBTOTAL for text?
You cannot "sum" text, but you can count it. Use function number 3 (or 103) for COUNTA, which counts any cell that is not blank, regardless of whether it contains text or numbers.
What is the difference between SUBTOTAL and AGGREGATE?
SUBTOTAL can ignore hidden rows, but it will return an error if any cell in the range contains an error. AGGREGATE can ignore hidden rows AND ignore error values, making it more robust for messy data.
Why is my SUBTOTAL showing 0?
This often happens if your numbers are stored as text. Excel cannot perform mathematical operations on text strings that look like numbers. Use the "Text to Columns" feature or the VALUE function to convert them to actual numbers.
Can I have multiple SUBTOTALs in one column?
Yes. You can have sub-totals for different sections of your data. When you create a final "Grand Total" using the SUBTOTAL function at the bottom, it will automatically ignore all the other SUBTOTAL formulas in that range to avoid double-counting.