Use the SUBTOTAL function to calculate sums, averages, or counts while automatically ignoring hidden rows or filtered data. The syntax is =SUBTOTAL(function_num, range). Select the function number from the list in the formula bar or type it directly (1 for AVERAGE, 9 for SUM) and specify the range of numbers to process.
What You'll Need
- A computer with Microsoft Excel installed (Excel 2010 or later recommended).
- A spreadsheet containing numerical data (e.g., sales figures, inventory counts, or test scores).
- Basic familiarity with selecting cell ranges and typing formulas.
Step-by-Step
- Prepare your data. Ensure your numbers are in a contiguous column or row. For example, place your sales figures in cells B2 through B20. Leave the header (e.g., "Sales") in cell B1.
- Decide on the calculation type. The SUBTOTAL function can perform many operations. For a simple sum, you need the number 9. For an average, use 1. For a count of numbers, use 102. You can find a full list of function numbers in Excel's Help documentation, but for most users, 9 (SUM), 1 (AVERAGE), and 102 (COUNT) are the most frequently needed.
- Click the cell where you want the result. This is where the subtotal will appear. It should be adjacent to your data, such as cell B21 if your data is in B2:B20.
- Start typing the formula. Type an equals sign (
=) followed bySUBTOTAL(. Excel will display the function name and open the AutoComplete list. - Select the function number. You can either type the number (e.g.,
9) or click it from the list that appears. The list will show the function name alongside the number (e.g., "SUM (9)"). Selecting from the list helps prevent typos. - Specify the range. After the closing parenthesis of the function number, type a comma (
,) to separate the arguments. Then, select the cells containing your data. You can do this by clicking and dragging your mouse over the cells, or by typing the range manually (e.g.,B2:B20) if you know the exact coordinates. - Close the formula. Type a closing parenthesis (
)to complete the function. - Press Enter. The result will appear in the selected cell. For the example above, the cell B21 will display the sum of the numbers in B2:B20.
Using the Subtotal Feature (The Ribbon Method)
Excel offers a built-in tool that adds subtotals to your list automatically. This method is excellent for large datasets where you want to break the data down by category (e.g., summing sales by region).
- Select all the data. Click on any cell within your dataset to ensure the entire range is selected.
- Open the Subtotal dialog. Go to the Data tab in the top ribbon. Click the Subtotal button in the Outline group. This opens a dialog box.
- Configure the settings. In the dialog box, you will see several options:
- At each change in: This is the most important setting. Select the column that defines your groups (e.g., "Region"). Excel will insert a subtotal each time the value in this column changes.
- Use function: Choose the calculation you want (e.g., Sum, Average, Count).
- Add subtotal to: Here, you can select which columns should be calculated. You can hold the Ctrl key to select multiple columns (e.g., Sales and Tax).
- Replace current subtotals: This option is checked by default. It overwrites any existing subtotals with the new ones. If you want to keep the old ones, uncheck this box.
- Summary using and Calculate: These options control how the grand total is calculated (sum of subtotals vs. sum of all raw data). The default is usually the sum of all raw data.
- Click OK. Excel will immediately calculate the subtotals. It will also add outlines to the left of your data, allowing you to collapse and expand the rows.
- Use the outline symbols. Look for the numbers 1, 2, and 3 on the left side of the sheet, next to the row numbers. Clicking 2 will show only the subtotals and grand totals. Clicking 3 will reveal all the data.
Common Mistakes
- Using the wrong function number. The most common error is using a standard SUM function (
=SUM()) instead of SUBTOTAL. If you filter your data to show only "North Region" sales, a standard SUM will still add the values from the hidden rows, giving you an incorrect total. SUBTOTAL ignores these hidden rows. - Incorrect range selection. Always ensure your range includes all the data you want to calculate, but not the subtotal itself if you are doing a running subtotal. If you select the cell containing the subtotal as part of the range, it will be included in the calculation, creating a recursive error. To avoid this, select the original data column, not the total column.
- Using the wrong calculation type. Confusing the function numbers can lead to unexpected results. For example, using 101 (COUNTA) counts all non-empty cells, including text, whereas 102 (COUNT) counts only cells with numeric values. Make sure you choose the specific operation you intend.
- Filtering before adding a subtotal. If you have already filtered your data to show only a subset, and then use the built-in Subtotal tool, Excel will treat the filtered rows as the source data. This can result in a subtotal that is lower than the original total, which can be confusing. It is best practice to apply the subtotal tool to the full, unfiltered dataset.
Tips
- Think of SUBTOTAL as a "Smart Sum". Its primary advantage is its ability to work seamlessly with Excel's filtering and hiding features. If you hide a row of data, the subtotal will automatically update to exclude that row. This makes it perfect for dynamic reports where data is frequently filtered.
- Use the Outline symbols to your advantage. The outline symbols on the left of the sheet are more than just for show. They allow you to quickly summarize complex data. Clicking the 2 button will hide the individual transaction rows and show you only the category totals. This is a great way to present a high-level view of your data.
- Combine it with other functions. While SUM is the most common use case, you can use SUBTOTAL for averages, counts, and even the maximum and minimum values. This allows you to create a dashboard of key metrics in a single column, all of which will behave correctly when rows are hidden.
- Be aware of the 100-109 series. Excel has two sets of function numbers for SUBTOTAL. The first set (1-11) includes functions that calculate based on visible rows. The second set (101-109) excludes hidden rows but also excludes nested SUBTOTAL functions. This means if you have a subtotal inside another subtotal, the inner one won't be counted if you use the 100+ series. For most users, the 1-11 series is the more intuitive choice.
Troubleshooting
If your SUBTOTAL formula isn't giving you the expected result, follow these steps to identify and fix the issue.
- Result is incorrect or too high. This is the most common error and usually indicates that you are using a standard function like
=SUM()instead of=SUBTOTAL(9, ...). Check your formula carefully and replace the SUM function with SUBTOTAL. Ensure that the function number (e.g., 9) is correct for your calculation. - Result is incorrect or too low. If your data contains hidden rows, this can happen if you used the wrong function number. For example, if you used
=SUBTOTAL(101, ...), it will ignore all hidden rows, including the ones you intended to include. Switch to the 1-11 series of function numbers to fix this. - The formula returns a #VALUE! error. This error usually means that the range you selected contains non-numeric data. SUBTOTAL can only perform mathematical operations on numbers. Check your selected cells to ensure they are all formatted as numbers. You can do this by highlighting the range and looking at the Number Format in the Home tab of the ribbon.
- The result doesn't update when I filter data. If your formula is static and doesn't change when you filter rows, it means you are using a standard function. The SUBTOTAL function is designed to update automatically. If you must use a standard function, you will need to manually recalculate or use a macro, but SUBTOTAL is the recommended solution.
- The built-in Subtotal tool creates too many subtotals. If you see multiple subtotals for the same category, it is likely because you applied the tool more than once without unchecking the "Replace current subtotals" box. Select all your data again, go to the Data tab, and click the Subtotal button. This time, uncheck the box and click OK. This will remove the old subtotals and start fresh.
Frequently Asked Questions
Does SUBTOTAL work with filtered data?
Yes, that is its main purpose. When you filter your spreadsheet, SUBTOTAL only calculates the visible numbers. If you hide rows manually, it also ignores them.
What is the difference between 9 and 109?
The difference is that 9 (and 1, 2, etc.) includes hidden rows, while 109 (and 101, 102, etc.) excludes them. The 100-series also excludes nested SUBTOTAL functions, which isn't usually an issue for simple calculations.
Can I use SUBTOTAL inside another SUBTOTAL?
You can, but you must be careful with the function numbers. If you use the 100-series (e.g., 109) for the outer function, it will ignore the inner function. For most reporting needs, you will only use one SUBTOTAL per cell.
How do I remove the subtotals?
Go to the Data tab and click the Clear button in the Outline group. Select Clear Subtotals from the dropdown menu. This will restore your data to its original, un-summarized state.
Why does my cell show #DIV/0!?
This error occurs when you are calculating an average (function number 1) and all the numbers in the range are zero or empty. The formula cannot divide by zero, so it displays this error.