How to Add Subtotal Formula in Excel
In this article
Yes, you can add a subtotal formula in Excel in just a few seconds by typing =SUBTOTAL(9, range) into a cell. This powerful function automatically sums visible data even when you filter your list or hide rows, preventing the common error of adding both hidden and visible numbers together.
What You'll Need
- A computer with Microsoft Excel installed (2016, 2019, 2021, or Microsoft 365). The steps are identical for both Windows and macOS versions.
- A dataset with numerical values organized in rows or columns. The formula works best when your data includes a header row.
- A basic understanding of cell references (e.g., A1:A10) or the ability to select a range with your mouse.
Step-by-Step
- Locate the cell where you want the subtotal to appear. This is typically at the bottom of the column you want to analyze, or in a dedicated summary area. For this example, assume you have sales figures in cells B2 through B10 and you want the total in cell B11.
- Type the equals sign (=). This signals to Excel that you are about to enter a formula. Click directly into cell B11 and press the equals key on your keyboard.
- Enter the function name. Type SUBTOTAL. As you type, Excel will suggest the function name in the formula bar or autocomplete list. You can either press the Tab key to select it or continue typing.
- Insert the function number (function_num). Type an opening parenthesis ( immediately after SUBTOTAL. Inside the parenthesis, you must specify the type of calculation you want to perform. For a standard sum, the code is 9. If you wanted an average, you would use 1. The full list of codes is available in the Excel Help menu, but for summation, 9 is the standard choice.
- Define the range. After the number, type a comma , to separate the function number from the range argument. Now, select your data range. You can do this by clicking and dragging your mouse over cells B2 through B10, or by typing the reference B2:B10. The formula should now look like
=SUBTOTAL(9, B2:B10). - Close the parenthesis. Type a closing parenthesis ) and press the Enter key. Excel will calculate the sum of the visible cells in the range B2:B10 and display the result in cell B11.
- Verify the result. To ensure the subtotal is working correctly with filters, click the filter icon (usually located in the header row of your data) and uncheck one or more items in the list. Notice that the subtotal number in B11 updates automatically to reflect only the remaining visible items.
Common Mistakes
One of the most frequent errors when adding a subtotal formula is using the standard SUM function, which is =SUM(B2:B10). While this creates a total, it does not account for filtered data. If you hide rows in your dataset, the SUM function will still include those hidden numbers in the total, potentially skewing your analysis. The SUBTOTAL function is designed specifically to ignore hidden rows and provide an accurate total of only the visible data.
Another mistake is using the wrong function number. For summation, ensure you are using 9. Using 109 instead of 9 will also sum the data but is less common in general use; 9 is the traditional identifier for SUM. It is also important to ensure that your range does not include the cell containing the subtotal formula itself, as this will create a circular reference error.
Tips
A major advantage of the SUBTOTAL function is its ability to handle subtotals within subtotals. If you have a large dataset and use the Data tab on the ribbon to insert a group or outline, you can use the SUBTOTAL function for the first level and then use the same function for the second level. Excel will automatically calculate the total of the smaller groups within the larger group without double-counting the data.
When working with filtered data, the SUBTOTAL function gives you a real-time snapshot of your current view. As you apply or remove filters, the total updates instantly, making it an excellent tool for what-if analysis. Additionally, the function works with other operations like counting (function number 2 or 102) and finding the maximum value (function number 4 or 104), providing flexibility beyond simple addition.
Troubleshooting
If the formula returns a #VALUE! error, it usually means that the range you selected contains text or non-numeric data. Excel cannot sum text, so you must ensure that your data range consists entirely of numbers. You can use the Find & Select feature (Ctrl + F) to locate and remove any text entries in the column.
If the result is incorrect or not updating when you filter the data, double-check that you are actually using the SUBTOTAL function and not the SUM function. Sometimes, copy-pasting formulas can inadvertently change the function name. Ensure the formula bar shows =SUBTOTAL(9, ...) and not =SUM(...). If the rows are visually hidden but not filtered, the SUBTOTAL function will still include them. You must use the filter feature to hide rows for the subtotal to exclude them.
Frequently Asked Questions
What is the difference between SUBTOTAL and SUM?
The primary difference is how they handle hidden or filtered rows. The SUM function adds all numbers in a range, regardless of whether they are hidden. The SUBTOTAL function only calculates visible numbers when you use the filter feature, making it essential for analyzing partial datasets.
Why does my SUBTOTAL formula show #DIV/0!?
A #DIV/0! error occurs when the function attempts to divide by zero. If you are using the AVERAGE function (function number 1), but your selected range contains no visible numbers, Excel cannot calculate an average and returns this error.
Can I use SUBTOTAL across multiple columns?
Yes, you can. You simply need to select the range that spans multiple columns. For example, to sum columns A, B, and C, you would select the range A2:C10. The function will add up all visible numbers within that wider selection.
What do the different numbers inside the parentheses mean?
The number inside the parentheses determines the operation. 9 represents SUM, 1 represents AVERAGE, 2 represents COUNT, and 3 represents COUNTA (counting non-empty cells). There are 11 functions in total, with 10 and 101 representing the same operations but ignoring nested subtotals.
Does SUBTOTAL work with 3-D references?
No, the SUBTOTAL function does not support 3-D references (referencing a specific range across multiple sheets). It is designed to work within a single, contiguous range on a single sheet.