To count filtered data in Excel, use the formula =SUBTOTAL(2, range) for numbers or =SUBTOTAL(3, range) for any non-empty cell. Unlike the standard COUNT function, SUBTOTAL ignores rows that have been hidden by a filter, ensuring your totals update dynamically as you change your view. This is essential for dashboards and reports where you need a live count of visible records rather than the entire dataset.
What You'll Need
- Microsoft Excel (any version from 2007 onwards)
- A dataset with at least one column of data to count
- A Filter applied to your data range (Data tab > Filter)
Step-by-Step
- Prepare Your Data: Ensure your data is organized in a table format with clear headers. For the SUBTOTAL function to work most effectively, your data should be a contiguous block without entirely empty rows or columns breaking the set.
- Apply a Filter: Select your header row. Go to the Data tab on the top ribbon and click the Filter button. You will now see drop-down arrows on each column header.
- Select the Result Cell: Click on the cell where you want the count to appear. It is generally best practice to place this cell above your data headers. If you place it below the data, the formula cell itself might be hidden when you filter the list, making the result invisible.
- Enter the Formula: Type the following formula into the cell:
- For counting numbers only:
=SUBTOTAL(2, A2:A100)(Replace A2:A100 with your actual data range). - For counting text or mixed data:
=SUBTOTAL(3, A2:A100)(Replace A2:A100 with your actual data range).
- For counting numbers only:
- Press Enter: The cell will now display the count of all visible rows in the specified range.
- Test the Filter: Click the filter drop-down arrow on any column and uncheck a few items. Notice that the count in your SUBTOTAL cell changes instantly to reflect only the rows currently visible on your screen.
Common Mistakes
One of the most frequent errors is using the wrong function number. The first argument in the SUBTOTAL function is a number that tells Excel which operation to perform. Users often confuse 2 and 3. Remember: 2 is for COUNT (numbers only), and 3 is for COUNTA (non-empty cells). If you use 2 on a column containing names or IDs with letters, your result will be 0.
Another common mistake is including the formula cell within the range being counted. For example, if your formula is in cell A1 and you set the range to A1:A100, you create a circular reference. This causes Excel to throw an error because the formula is trying to count itself. Always ensure the range starts below the cell containing the formula.
Users also frequently mistake SUBTOTAL for a formula that handles manually hidden rows by default. By default, =SUBTOTAL(3, range) ignores rows hidden by a Filter, but it will still count rows that you have hidden manually (by right-clicking a row and selecting Hide). To ignore both filtered-out and manually hidden rows, you must use the 100-series function numbers, such as =SUBTOTAL(103, range).
Tips
To make your range dynamic, convert your data range into an official Excel Table. Select your data and press Ctrl + T. Once it is a table, you can use structured references in your formula (e.g., =SUBTOTAL(3, Table1[Customer Name])). This means that as you add new rows to the bottom of the table, the SUBTOTAL formula updates automatically without you having to manually change the range from A100 to A101.
Combine SUBTOTAL with a clear label. Instead of just having a number in a cell, use a formula to create a descriptive label: ="Visible Records: " & SUBTOTAL(3, A2:A100). This makes your spreadsheet much more professional and easier for other users to understand when they are filtering the data.
If you need to perform multiple counts based on different criteria without filtering, consider using COUNTIFS. However, COUNTIFS cannot ignore hidden rows. If your goal is a "Live Count" of what the user sees, SUBTOTAL is the only native function that achieves this without complex VBA macros.
Troubleshooting
The count is not changing when I filter: First, check if you are using the standard COUNT or COUNTA function instead of SUBTOTAL. Standard count functions always look at the entire range regardless of visibility. Second, ensure that you have actually applied a Filter to the data. If you simply hid rows manually without using the Filter tool, SUBTOTAL(3, ...) will still count them. Switch to SUBTOTAL(103, ...) to ignore manually hidden rows.
The result is 0, but I see data: This usually happens when using function number 2 on a column that contains text. Function 2 only counts cells containing numeric values. If your column contains text, dates stored as text, or alphanumeric IDs, change the first argument from 2 to 3.
I see a #REF! error: This occurs if the range referenced in your formula has been deleted. If you deleted columns or rows that were part of the A2:A100 range, Excel no longer knows where to look. Re-select your range by clicking and dragging over the cells while typing the formula.
The formula is returning a #VALUE! error: This is rare for SUBTOTAL, but it can happen if the range is invalid or if the function number is outside the 1-11 or 101-111 range. Ensure your first argument is exactly 2, 3, 102, or 103.
Frequently Asked Questions
What is the difference between SUBTOTAL(3, ...) and SUBTOTAL(103, ...)?
SUBTOTAL(3, ...) ignores rows hidden by a filter but counts rows that were hidden manually. SUBTOTAL(103, ...) ignores both filtered-out rows and rows that were hidden manually via the right-click menu.
Can SUBTOTAL count unique values?
No, SUBTOTAL cannot count unique values on its own. To count unique visible values, you would need a more complex array formula combining SUMPRODUCT, SUBTOTAL, and COUNTIF, or use a Pivot Table with the "Add this data to the Data Model" option checked to enable the Distinct Count feature.
Does SUBTOTAL work with grouped rows?
Yes, but it depends on the function number. If you use the 100-series (like 103), it will ignore rows that are collapsed within a Group. If you use the single-digit series (like 3), it will count them unless they are hidden by a filter.
Why use SUBTOTAL instead of COUNT?
The standard COUNT function always calculates the total for the entire range, even if you have filtered the list to show only one specific category. SUBTOTAL allows you to see the count of only the data currently visible on your screen.
Can I use SUBTOTAL for other things besides counting?
Yes, the SUBTOTAL function is versatile. By changing the first number, you can calculate the Sum (9 or 109), Average (1 or 101), Maximum (4 or 104), and Minimum (5 or 105) of visible cells.