The SUBTOTAL function in Excel 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 whether it is visible, SUBTOTAL dynamically updates its result to reflect only the data currently displayed on your screen. This makes it the essential tool for creating interactive reports and dashboards where users need to slice data and see instant totals for the filtered subset.
What You'll Need
- Microsoft Excel installed on your computer (available in all modern versions including Office 365, Excel 2021, 2019, and 2016).
- A dataset organized in a tabular format with clear column headers.
- Data that is either already formatted as an Excel Table (Ctrl + T) or has the Filter feature enabled via the Data tab.
Step-by-Step
- Prepare Your Data Range: Ensure your data is organized in rows and columns. For the SUBTOTAL function to work effectively with filters, your data should not have completely empty rows or columns within the dataset. Select your entire data range, including the headers.
- Enable Filtering: Go to the Data tab on the top ribbon and click the Filter button. You will see drop-down arrows appear in the header cell of each column. This allows you to hide specific rows, which is where the SUBTOTAL function proves its value.
- Select the Target Cell: Click on the cell where you want the total to appear. It is standard practice to place this cell either directly above your headers or at the very bottom of your data column. If you place it at the bottom, ensure you leave a few empty rows below it so that when you filter the data, the total row doesn't get hidden along with the data.
- Start the Formula: Type
=SUBTOTAL(into the cell. You will notice Excel provide a tooltip showing the syntax:SUBTOTAL(function_num, ref1, [ref2], ...). - Choose Your Function Number: The first argument is the
function_num. This is a number from 1 to 11 (or 101 to 111) that tells Excel which calculation to perform. Common codes include:- 9 or 109: SUM (Adds the values)
- 1 or 101: AVERAGE (Calculates the mean)
- 2 or 102: COUNT (Counts cells containing numbers)
- 3 or 103: COUNTA (Counts non-empty cells)
- 4 or 104: MAX (Finds the highest value)
- 5 or 105: MIN (Finds the lowest value)
9is the most common choice for totaling a column. - Select the Data Range: After the comma, click and drag to select the range of cells you want to calculate. For example, if your sales figures are in cells B2 through B100, your formula would look like
=SUBTOTAL(9, B2:B100). - Close and Enter: Type the closing parenthesis
)and press Enter. - Test the Filter: Use the filter arrows in your headers to hide certain categories or values. Observe the SUBTOTAL cell; the value will change instantly to reflect only the visible rows. If you had used a regular SUM formula, the total would remain the same regardless of the filter.
Common Mistakes
One of the most frequent errors is using the SUBTOTAL function on a range that includes the cell where the formula itself is located. This creates a Circular Reference error, which causes Excel to display a warning and return a value of zero. Always ensure your range starts below your total cell or ends above it.
Another common mistake is confusing the 1-11 function codes with the 101-111 codes. While both work with filters, they behave differently regarding manually hidden rows. If you right-click a row and select Hide, the 1-11 series will still include those hidden values in the total. The 101-111 series will ignore them. Many users use the 1-11 series and then wonder why their "hidden" rows are still being counted; switching to 109 instead of 9 usually solves this.
Users also frequently apply the SUBTOTAL formula to a range that isn't actually filtered. While the formula works on unfiltered data (acting exactly like a SUM or AVERAGE), the primary benefit is lost. Ensure your data is converted to a Table or has filters active to maximize the utility of the function.
Tips
Use Excel Tables for Automation: If you convert your data range into an official Table (select data and press Ctrl + T), you can enable a "Total Row" with a single click. Go to the Table Design tab and check the Total Row box. Excel automatically inserts SUBTOTAL formulas at the bottom of your columns, and you can change the calculation (Sum, Average, Count) via a drop-down menu without typing any formulas.
Combining SUBTOTAL with Other Functions: You can nest SUBTOTAL inside other formulas. For example, if you want to calculate the percentage of the total that a specific filtered item represents, you can divide a cell value by a SUBTOTAL formula. Because the SUBTOTAL changes as you filter, your percentage calculation will remain relative to the currently visible data.
Avoid Double Counting: If you have multiple SUBTOTAL formulas within a single range (for example, subtotals for different regions and then a grand total at the bottom), the grand total SUBTOTAL formula is smart enough to ignore other SUBTOTAL formulas within its range. This prevents the common error of doubling your totals.
Troubleshooting
The result is not changing when I filter: Check if you are using the standard SUM function instead of SUBTOTAL. If you are using SUBTOTAL, ensure that you are using the Filter tool (Data > Filter) and not just manually hiding rows using the 1-11 function codes. If you manually hid rows, you must use the 101-111 series of codes.
I see a #VALUE! error: This usually happens if the range you selected contains cells with errors (like #N/A or #DIV/0!). The SUBTOTAL function cannot calculate a range that contains an error. You must first clean your data or use the AGGREGATE function, which is a more advanced version of SUBTOTAL that can be set to ignore error values.
The total seems too high: Check for circular references. If your formula range includes the cell the formula is in, the calculation will be incorrect. Also, check if you have manually entered numbers in the range that you thought were formulas; SUBTOTAL only ignores other SUBTOTAL functions, not hard-coded numbers that look like totals.
The formula is returning 0: Ensure that the numbers in your range are actually formatted as numbers and not as text. If numbers are stored as text (often indicated by a small green triangle in the corner of the cell), the SUBTOTAL(9, ...) function will ignore them because it only sums numeric values.
Frequently Asked Questions
What is the difference between SUBTOTAL and SUM?
SUM adds every cell in the specified range regardless of whether the row is hidden or filtered. SUBTOTAL only adds the cells that are visible after a filter has been applied, making it dynamic.
Can I use SUBTOTAL for things other than adding?
Yes. By changing the first number in the formula, you can perform 11 different operations, including counting (COUNTA), finding the average (AVERAGE), and identifying the maximum or minimum values in a filtered list.
Why should I use 109 instead of 9?
Function 9 ignores rows hidden by a Filter, but it still includes rows that were hidden manually (by right-clicking a row and selecting Hide). Function 109 ignores both filtered-out rows and manually hidden rows.
Does SUBTOTAL work with the AGGREGATE function?
They are similar, but AGGREGATE is more powerful. While SUBTOTAL can ignore hidden rows, AGGREGATE can ignore hidden rows, error values, and other nested AGGREGATE or SUBTOTAL functions simultaneously.
Can SUBTOTAL be used across multiple worksheets?
SUBTOTAL is designed for a single contiguous range on one sheet. To total filtered data across multiple sheets, you would typically need to use a Pivot Table or Power Query to consolidate the data first.