Yes, you can nest the COUNTIF function inside SUBTOTAL to create a dynamic total that adjusts when you filter your data. This technique allows you to count specific items within the visible rows of a filtered list, ignoring hidden ones. To do this, you must use the function number 5 for Subtotal, which counts numeric values, and wrap your COUNTIF criteria inside the SUBTOTAL formula.

What You'll Need

  • A spreadsheet program such as Microsoft Excel or Google Sheets.
  • A dataset containing at least one column of data you wish to filter and a second column with values or text to count.
  • A computer or mobile device with the spreadsheet application installed.

Step-by-Step

  1. Prepare your data. Organize your information in a standard table format. Ensure the column you want to filter (e.g., a Category or Region column) is in one column, and the column you want to count (e.g., a Status or Amount column) is in another. Excel will automatically attempt to create a Table from your data if it detects headers; if it does not, you can manually select your data and press Ctrl+T to turn it into an official Table object.
  2. Locate the SUBTOTAL function. Go to the cell where you want the result to appear. On Windows, click the Formulas tab on the ribbon and select Insert Function. Alternatively, type "=SUBTOTAL(" directly into the cell. On a Mac, you can also access this via the Formulas menu or simply typing the function name.
  3. Enter the function number. The SUBTOTAL function requires a first argument that determines which operation to perform. For counting, you must use the number 5. Typing this number tells Excel to count only the visible cells in the range, ignoring any that are hidden by filters or manually hidden rows.
  4. Identify the reference range. The second argument is the range of cells to analyze. Click and drag your mouse over the column containing the values you want to count. This range includes the criteria column (the first argument of your COUNTIF) and the data column (the second argument of your COUNTIF). For example, if you are counting "Sold" items in a list where column A contains the status and column B contains the item name, you would drag from A1 to B100.
  5. Nest the COUNTIF criteria. After the closing parenthesis of your reference range, type a comma and then open a new parenthesis for the COUNTIF function. The syntax now looks like this: =SUBTOTAL(5, COUNTIF(range, criteria)). The range here should match the data column you selected in the previous step, not the combined reference range.
  6. Define the criteria. Enter the specific text or value you want to count, enclosed in quotation marks. For example, if you are counting orders marked as "Complete", you would type "Complete". If you are counting numbers greater than a certain value, you would type >100.
  7. Close the formula. Finish by closing the inner parenthesis for the COUNTIF function, followed by the closing parenthesis for the SUBTOTAL function. The final formula will resemble: =SUBTOTAL(5, COUNTIF(B2:B100, "Complete")).
  8. Apply a filter. Click anywhere inside your data table. Navigate to the Data tab on the ribbon and click the Filter button (the funnel icon). Click the dropdown arrow on the header of the column you wish to filter (e.g., Region). Uncheck the boxes for the regions you want to exclude and click OK.
  9. Verify the result. Look at the SUBTOTAL formula cell you created. As you change the filter selections, the number in this cell will update to reflect only the count of your specified criteria within the visible rows.

Common Mistakes

One of the most frequent errors is using the wrong function number in the SUBTOTAL formula. If you use number 1, 2, or 3, Excel will count all cells, including those that are hidden by filters. This defeats the purpose of using SUBTOTAL in the first place. Always ensure the first argument is 5 (COUNT) or 109 (COUNTA) to respect the visibility of the data.

Another common issue arises from incorrect range nesting. Beginners often try to nest the entire range of the table into the SUBTOTAL function, including the header row, which causes the formula to return an error. It is crucial to strip the header row from the range reference. Always reference the specific data cells, typically starting from row 2 or 3, rather than the whole column.

Tips

Use structured references if you have converted your data into an official Excel Table (Ctrl+T). Instead of typing cell addresses like A2:A100, you can refer to the table by name, such as Table1[Status]. This makes your formulas easier to read and automatically adjusts when you add new rows of data.

When counting text values that are not empty, use the number 103 for SUBTOTAL. This corresponds to the COUNTA function, which counts non-blank cells. This is useful when you want to filter out blank rows and see how many items have data populated in them.

Troubleshooting

If the formula returns a #VALUE! error, check your parenthesis balance. Excel formulas are strict about matching opening and closing parentheses. Click inside the cell with the error and use the formula auditing tools on the Formulas tab, such as "Evaluate Formula", to step through the calculation and see exactly where the syntax breaks down.

If the result is not updating when you filter the data, verify that you are actually in Filter view. Sometimes, filters can be applied to individual columns rather than the whole list, or the table might not be recognized as a valid data range. Ensure the Data tab is active and the filter icon on your column headers is highlighted, indicating filtering is enabled.

Frequently Asked Questions

Why does my SUBTOTAL formula ignore my hidden rows?

It does not ignore them; it specifically counts only the visible ones. The SUBTOTAL function with function number 5 is programmed to skip cells that have been hidden by the Filter feature or manually hidden via the Row > Hide command.

Can I use SUBTOTAL with COUNTIF on a Mac?

Yes, the syntax and logic are identical on macOS. The menu paths may differ slightly, but the formula structure =SUBTOTAL(5, COUNTIF(range, criteria)) works the same way in both Excel for Mac and Excel for Windows.

What does the number 5 mean in the SUBTOTAL formula?

The number 5 corresponds to the COUNT function in Excel's internal list of function numbers. This tells Excel to count only the numeric values in the specified range that are not hidden by filters.

Can I filter by multiple criteria at once?

The SUBTOTAL function itself cannot apply multiple criteria like the COUNTIFS function. You must use the filter dropdowns on your data columns to select multiple items (e.g., select both "East" and "West" from the Region dropdown). The SUBTOTAL formula will then count the items that match any of your visible filters.

How do I count based on a date range?

You can nest the COUNTIF criteria with date operators. For example, to count items after a specific date, use >=1/1/2023. The SUBTOTAL function will then count only the visible rows that fall within that date range.

Does this work with Google Sheets?

Yes, this functionality is fully supported in Google Sheets. The keyboard shortcuts and menu options vary, but the formula syntax is identical and the filtering behavior is the same.