Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Subtotal for Filtered Data in Excel

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Yes, you can calculate a subtotal for filtered data in Excel using the SUBTOTAL function. This function automatically ignores rows hidden by a filter when performing calculations, ensuring your total only reflects the visible data. It replaces the need to manually hide and unhide rows or use complex formulas to exclude filtered-out items. This guide shows you exactly how to set it up on a desktop.

What You'll Need

  • A computer with Microsoft Excel installed (the steps below apply to Windows versions of Excel, including Excel 2016, 2019, and Microsoft 365).
  • A dataset with at least two columns: one containing the data you want to sum or average, and one containing the category or filter criteria.
  • A basic understanding of how Excel filters work (e.g., using the Filter button on the Data tab).

Step-by-Step

  1. Open your dataset in Excel. Ensure your data is organized in a standard table format. For this example, imagine you have a list of sales transactions in columns A and B, where Column A contains the product names and Column B contains the sales amounts. There should be no blank rows within your data range.
  2. Select your data. Click on any cell within your dataset. You can select the entire range of data (e.g., A1:B100) or just the column containing the numbers you want to calculate (e.g., B1:B100). Selecting the entire range is often safer as it allows you to apply the function to any column.
  3. Insert the SUBTOTAL function. Go to the Data tab on the Excel ribbon at the top of the window. Look for the group labeled Sort & Filter. Click the AutoSum button (it looks like the Greek letter sigma, Σ). This is the fastest way to insert a SUM function. Excel will automatically guess the range of numbers and insert a SUM formula at the bottom of your selection.
  4. Change the SUM function to SUBTOTAL. With the newly inserted formula selected (it should be in the cell immediately below your last data row), click into the formula bar at the top of the window. You will see it currently says something like `=SUM(B2:B99)`. You need to change the word SUM to SUBTOTAL. The formula should now read `=SUBTOTAL(109,B2:B99)`. The number 109 is the function code for SUM (1 is for AVERAGE, 101 for AVERAGE, etc.). Do not manually type the function code if you used AutoSum, as it might insert an incorrect code like 1 instead of 109.
  5. Filter your data. Go back to the Data tab and click the Filter button. You will see little drop-down arrows appear in the headers of your selected columns. Click the drop-down arrow on the header of the column you want to filter by (e.g., the Product Name column).
  6. Apply a filter selection. In the menu that appears, uncheck the Select All box at the top. Now, check only the specific items you want to include in your subtotal. For example, if you only want to see transactions for "Product A" and "Product B", check those boxes while leaving others unchecked. Click OK to apply the filter.
  7. Verify the result. Look at the subtotal cell you created in step 3. The number in this cell should now update to reflect only the sum of the visible rows that match your filter criteria. If you uncheck all boxes, it will return 0. If you filter for a single item, it will show the sum of that item only.
  8. Turn off the filter. Once you are done, you can click the Filter button again to remove the filter and see all your data. The SUBTOTAL formula will remain in place, ready for the next calculation.

Common Mistakes

  • Using the wrong function code. Excel offers two sets of SUBTOTAL function codes. Codes 1, 101, 201, etc., are for functions that ignore hidden rows (like AVERAGE, SUM, COUNT, MAX). Codes 2, 102, 202, etc., are for functions that include hidden rows (like COUNTA, STDEV, VAR). When filtering data, you almost always want to use the first set (e.g., 109 for SUM) to exclude the hidden rows. If you use code 9, it will be the same as SUM, but code 109 is the standard for this purpose in the AutoSum workflow.
  • Filtering after the formula. A common error is creating the formula first and then trying to filter the data. If you have a formula in a separate row outside your data range (e.g., a total at the very bottom of the sheet) and you try to filter the data below it, the formula will not update. The SUBTOTAL function must be placed within the data range or immediately adjacent to the filtered data for it to work correctly. The AutoSum feature is designed to place the formula in the correct location.
  • Using SUM instead of SUBTOTAL. While SUM works for simple totals, it counts all rows, including those hidden by a filter. This will give you an incorrect total for your filtered view. Always use SUBTOTAL when you need to calculate a total based on visible data.

Tips

  • Use the AutoSum button for speed. The AutoSum button is the most efficient way to create a subtotal. It not only inserts the correct formula, but it also automatically detects the range of numbers below the active cell. This prevents errors from selecting the wrong range of cells.
  • Use different function codes for different calculations. You are not limited to SUM. You can use the same SUBTOTAL formula to calculate an average, a count of items, a maximum or minimum value, or a standard deviation. Simply change the function code in the formula bar (e.g., 1 for AVERAGE, 2 for COUNT, 3 for COUNTA, 4 for MAX, 5 for MIN).
  • Use the SUBTOTAL function for grouping. You can use the SUBTOTAL function with the 101-109 function codes to create hierarchical summaries. For example, you can have a grand total at the bottom of the sheet, and then, for each category, you can have a subtotal. If you filter the data by a specific category, the grand total will update to show the sum of only that category, and the category subtotal will remain visible.
  • Check your filter selection. If your subtotal is 0, check that you have actually selected at least one item in the filter dropdown. Also, ensure that there are no blank cells in your data range, as blanks can sometimes be excluded from calculations depending on the specific filter settings.

Troubleshooting

If your subtotal is not updating correctly, there are a few things you can check. First, ensure that you have actually applied a filter. If the filter is not active, the SUBTOTAL function will behave like a standard SUM and include all rows. Second, verify that you are using a valid function code, such as 109 for SUM. If you are seeing a #VALUE! error, check that your data range does not contain non-numeric text, as this can cause the function to fail. If the result is not what you expect, double-check that you are not including any hidden rows in your calculation range.

Frequently Asked Questions

Can I use SUBTOTAL with other functions like IF or VLOOKUP?

Yes, you can nest the SUBTOTAL function inside other functions. For example, you could use `=SUBTOTAL(109,IF(A1:A10>100,A1:A10,0))` to sum only the values greater than 100. However, this is more complex and is rarely needed if you can achieve the same result by filtering your data.

Does the SUBTOTAL function work with grouped rows?

Yes, the SUBTOTAL function is designed to work with grouped rows. If you have rows grouped and you collapse a group, rows within that group are considered "hidden" and will be ignored by the SUBTOTAL function.

What is the difference between 109 and 1 in the SUBTOTAL function?

The number 109 is the function code for SUM that ignores hidden rows. The number 1 is the function code for AVERAGE that ignores hidden rows. The difference is the function itself, not whether it ignores hidden rows. The key distinction is between the 100-series codes (which ignore hidden rows) and the 200-series codes (which include hidden rows).

Why does my subtotal change when I sort my data?

Your subtotal should not change when you sort your data, provided the filter is still active. If it does change, it is likely because the formula is placed outside of the filtered range or the filter was accidentally turned off during the sort operation.

Can I use SUBTOTAL on a pivot table?

Yes, pivot tables have their own built-in subtotal functionality. You can enable or disable subtotals for specific fields in the PivotTable Options. The SUBTOTAL function is primarily used for standard data ranges, not for pivot tables.