How to Use the SUBTOTAL Function in Excel
In this article
Yes, the SUBTOTAL function in Excel is the correct tool for calculating sums, averages, and counts while dynamically ignoring hidden rows and other subtotals in a list. It allows you to perform different operations on a range without double-counting data that is hidden by filters or manually row hiding.
What You'll Need
- A computer running Microsoft Excel (2016, 2019, or Microsoft 365 are recommended for the best features).
- A spreadsheet containing numerical data that you wish to analyze.
- A basic understanding of how to select ranges of cells and drag the fill handle.
Step-by-Step
- Locate your data. Open your Excel workbook and navigate to the worksheet containing the data you want to sum. Ensure your data is organized in a clean list format, ideally with column headers at the very top, as this helps Excel identify the range correctly.
- Decide on the location. Click on the cell where you want the subtotal result to appear. This cell should be adjacent to your data or in a summary area at the bottom of your list, but not within the data range itself to avoid circular references.
-
Enter the function. Type the equals sign (=) followed by the word
SUBTOTAL. You can also select the function from the Formulas tab on the ribbon by clicking theAutoSumdropdown arrow and choosingMore Functions, then searching for SUBTOTAL. However, typing it manually is often faster. -
Specify the function number. Immediately after typing
SUBTOTAL(, you must enter a number that represents the specific calculation you want to perform. This is the most critical part of the function. Do not confuse this number with the actual result. Common numbers include:- 1 for AVERAGE.
- 2 for COUNT (counts cells with numbers).
- 3 for COUNTA (counts non-empty cells).
- 4 for MAX.
- 5 for MIN.
- 6 for PRODUCT.
- 9 for SUM (the most common usage).
- 10 for STDEV.S (standard deviation).
- 11 for VAR.S (variance).
Note: The first
1in the list applies to the range of visible cells only. The second1in the list applies to all cells, including hidden ones. -
Define the range. After the function number, type a comma (,) to separate the inputs. Next, click and drag your mouse to select the range of cells you want to include in the calculation. For example, if your numbers are in cells B2 through B20, you would type
B2:B20. -
Close the parenthesis. Type the closing parenthesis
)and press Enter. Excel will calculate the subtotal based on your selected function number and range. - Verify with filters. To prove that the SUBTOTAL function is working correctly, select the header row of your data and go to the Data tab on the ribbon. Click the Filter button (an icon that looks like a funnel). Click the dropdown arrow on one of your column headers (e.g., the 'Month' header) and uncheck a few items to hide those rows. Observe that your SUBTOTAL result in the summary cell updates automatically to exclude the hidden data.
Common Mistakes
One of the most frequent errors users make is using the wrong function number. Forgetting the specific number for SUM (which is 9) or AVERAGE (which is 1) will result in an error or a completely incorrect calculation. Always double-check that the first digit matches your desired operation.
Another common issue arises when nesting SUBTOTAL functions. If you try to use a SUBTOTAL formula to calculate the sum of a column that already contains a subtotal, you may end up double-counting the data. The SUBTOTAL function is designed to ignore other SUBTOTAL functions within the range, but this behavior can sometimes be confusing if you are not aware of how Excel handles nested calculations.
Tips
When working with very large datasets, typing the function number manually can be prone to typos. A useful tip is to use the Insert Function dialog box. You can access this by clicking the fx button next to the formula bar. In the dialog, Excel will list the function numbers alongside their descriptions, allowing you to select the correct one without memorizing the codes.
It is also important to remember that the SUBTOTAL function works differently depending on how you hide rows. If you hide rows using the Filter feature (Data tab > Filter), the SUBTOTAL function will ignore them. However, if you manually hide rows by right-clicking a row number and selecting Hide, the SUBTOTAL function will still include those hidden rows in its calculation. This is because filtering changes the view, while hiding changes the data structure.
Troubleshooting
If your SUBTOTAL function returns a #VALUE! error, it is usually because the range you selected contains text or non-numeric data where a numeric calculation is expected. For example, if you try to calculate the AVERAGE of a range that includes a text string, Excel cannot perform the math and will throw an error. To fix this, ensure that all cells in the range you are summing contain numbers, or use the COUNTA function (function number 3) if you simply want to count non-empty cells.
If the result is incorrect, check that the function number is correct. For instance, if you want to count numbers but accidentally use function number 3 (COUNTA), you will get a higher number than expected because it counts blank cells as well. Ensure the first digit of your function code matches exactly what you need.
If the result does not change when you filter the data, verify that the filter is applied correctly. Go to the Data tab and ensure the Filter button is highlighted (turned blue). If it is not highlighted, click it to enable filtering on your worksheet.
Frequently Asked Questions
What is the difference between SUM and SUBTOTAL?
The SUM function simply adds up all the numbers in a range. The SUBTOTAL function can add up, average, count, and perform many other mathematical operations, but its unique feature is that it automatically ignores cells that are hidden by a filter. If you filter a list and use SUM, the result will not change; if you use SUBTOTAL, it will automatically update to reflect only the visible data.
Does SUBTOTAL work on filtered columns?
Yes. When you apply a filter to a column, the SUBTOTAL function is designed to calculate the result based only on the visible rows. This makes it extremely useful for creating dynamic dashboards where you want to see the total of a specific subset of data without having to manually create separate areas for each group.
Why is my SUBTOTAL formula giving me a #DIV/0! error?
A #DIV/0! error occurs when you are trying to calculate an average or a standard deviation, and the range contains no numeric data. Since you cannot divide by zero, Excel returns this error. To fix this, either ensure there is at least one number in the range or use the COUNT function to check if data exists before running the calculation.
Can I use SUBTOTAL with 3D references?
No, you cannot. The SUBTOTAL function does not support 3D references (referring to the same cell or range on multiple sheets). If you need to calculate a subtotal across multiple sheets, you must either use the SUM function or create a summary sheet that pulls the individual totals from each sheet into a single range.
What is the function number for STDEV.P (population standard deviation)?
The function number for STDEV.P is 101. The first digit (1) indicates it is a statistical function, and the second digit (01) specifies it is the population standard deviation. Similarly, 102 is for VAR.P (population variance), and 103 is for STDEV.S (sample standard deviation).