The SUBTOTAL function in Excel is a versatile tool 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 or AVERAGE functions, which calculate every cell in a selected range regardless of whether they are visible, SUBTOTAL dynamically updates to reflect only the data currently displayed on your screen. This makes it an essential tool for anyone working with large datasets who needs to generate reports based on specific filtered criteria without manually changing formulas every time the filter changes.
What You'll Need
- Microsoft Excel installed on your computer (compatible with Excel 97 through the latest versions).
- A dataset organized in a tabular format with clear column headers.
- Data that is either already filtered or ready to be converted into a filterable range.
Step-by-Step
- Prepare Your Data Range: Ensure your data is organized in a clean table. Each column should have a unique header (e.g., "Date," "Category," "Amount"), and there should be no completely empty rows or columns within the dataset. This ensures Excel recognizes the boundaries of your data correctly.
- Apply a Filter to Your Headers: Select the header row of your data. Go to the Data tab on the top ribbon and click the Filter button. You will now see small drop-down arrows appearing in each header cell. This step is critical because the SUBTOTAL function's primary power is its ability to react to these filters.
- Select the Destination Cell: Click on the cell where you want the result to appear. It is generally best practice to place the SUBTOTAL formula either above the header row or below the very last row of your data. Placing it below the data allows you to see the total as you scroll, but placing it above prevents it from being hidden when you apply a filter to the columns.
- Enter the Function Syntax: Begin typing the formula:
=SUBTOTAL(. The function requires two arguments: a function number and a range. - Choose Your Function Number: After the opening parenthesis, enter a number from 1 to 11 (or 101 to 111) to tell Excel which calculation to perform. For example, if you want to sum the values, use 9. If you want to average them, use 1. If you want to count the cells containing numbers, use 2. Separate this number with a comma.
- Define the Range: After the comma, click and drag your mouse to select the range of cells you want to calculate (e.g.,
B2:B100). Close the parenthesis and press Enter. Your formula should look something like=SUBTOTAL(9, B2:B100). - Test the Filter: Use the drop-down arrows in your header row to filter your data. For instance, if you have a "Category" column, filter for only "Office Supplies." You will notice that the SUBTOTAL result changes instantly to reflect only the sum of the visible "Office Supplies" rows, whereas a standard SUM formula would continue to show the total for the entire list.
Common Mistakes
One of the most frequent errors is using the SUBTOTAL function within the range that the function itself is calculating. This creates a circular reference, where the formula tries to calculate its own value, leading to an error message and an incorrect result of zero. Always place your SUBTOTAL formula outside the range of the data cells you are referencing.
Another common mistake is confusing the function numbers. Users often forget that the number 9 is for SUM and 1 is for AVERAGE. Entering the wrong number will lead to a mathematically correct result for the wrong operation—for example, seeing a count of 50 items when you were expecting a total dollar amount of $5,000.
Many users also forget that SUBTOTAL only ignores rows hidden by a filter, not rows hidden manually by right-clicking and selecting "Hide." If you manually hide rows, you must use the 100-series function numbers (e.g., 109 instead of 9) to ensure those manually hidden rows are excluded from the calculation.
Finally, applying SUBTOTAL to a range that includes non-numeric data (like text strings) can sometimes lead to unexpected results if you are using the COUNT function. Ensure the range selected contains the data type appropriate for the function number you have chosen.
Tips
To make your spreadsheets more professional, combine the SUBTOTAL function with a Named Range. Instead of referencing B2:B100, you can name that range "SalesData." Your formula becomes =SUBTOTAL(9, SalesData), which is much easier to read and maintain as your dataset grows.
If you are working with a very large dataset, consider converting your data range into an official Excel Table (Insert > Table). When you use a table, you can enable a "Total Row" at the bottom. Excel automatically inserts SUBTOTAL functions into this row, and you can change the calculation (Sum, Average, Count) using a simple drop-down menu without typing any formulas at all.
For those who need to perform multiple types of analysis on the same data, create a "Summary Dashboard" at the top of the sheet. Place a SUM subtotal, an AVERAGE subtotal, and a COUNT subtotal side-by-side. This allows you to see a comprehensive snapshot of your filtered data at a glance without scrolling to the bottom of the page.
Remember that the 100-series function numbers (101-111) are the "strictest" versions of the function. They ignore everything that isn't visible: filtered-out rows AND manually hidden rows. If you want your report to be absolutely accurate regardless of how a row was hidden, always default to the 100-series.
Troubleshooting
If your SUBTOTAL formula is returning a #VALUE! error, check the range you have selected. This usually happens if the range contains an error value (like #N/A or #DIV/0!) within the cells. You must fix the errors in the source data before the SUBTOTAL function can process the range.
If the result is not changing when you apply a filter, check your Calculation Options. Go to the Formulas tab and look at the Calculation section. If it is set to "Manual," the formula will not update until you press F9 or save the workbook. Change this setting to "Automatic" to ensure the subtotal updates in real-time as you filter.
If the result seems too high, verify that you haven't accidentally included the SUBTOTAL cell itself in the range. For example, if your formula is in cell B101 and your range is B2:B101, you have a circular reference. Change the range to B2:B100.
If the formula is returning 0 but you can clearly see numbers in the filtered list, ensure that your numbers are actually stored as numbers and not as text. You can tell if a number is stored as text if it is left-aligned in the cell or has a small green triangle in the top-left corner. Use the "Text to Columns" feature or the VALUE function to convert them back to numbers.
Frequently Asked Questions
What is the difference between SUBTOTAL(9, ...) and SUBTOTAL(109, ...)?
Both perform a SUM. However, 9 ignores rows hidden by a filter but includes rows you have hidden manually. 109 ignores both filtered-out rows and manually hidden rows.
Can I use SUBTOTAL for counting unique values?
No, the SUBTOTAL function does not have a built-in option for unique counts. You would need to use a combination of SUMPRODUCT and COUNTIF or use a Pivot Table for unique counts.
Does SUBTOTAL work with the SUMIFS function?
No, they are different tools. SUMIFS calculates based on criteria you define in the formula, while SUBTOTAL calculates based on what is currently visible on the screen due to filtering.
Will SUBTOTAL work if I have multiple columns of data?
Yes, but the range argument must refer to a single column of numbers for the calculation to make sense. You cannot sum a 2D block of cells if you intend to use it as a dynamic filtered total for a specific category.
Can I nest a SUBTOTAL function inside another formula?
Yes, you can use a SUBTOTAL result as part of a larger calculation, such as dividing a SUBTOTAL sum by a fixed constant to calculate a tax or percentage.