The SUBTOTAL function combined with COUNTIF lets you count visible items in filtered data while ignoring hidden rows—perfect for dynamic summaries in large datasets. This combo works only when you apply Excel filters and need counts that update automatically as you filter.
What You'll Need
- A Microsoft Excel workbook (2010 or later)
- A table or range of data with headers and at least one column of criteria to count
- Basic familiarity with Excel filters (Data > Filter)
- Excel must be in calculation mode set to Automatic (File > Options > Formulas > Calculation Options)
Step-by-Step
- Open your Excel file and select the range of data you want to analyze. Ensure your data has column headers (e.g., "Region", "Status", "Sales").
- Go to the Data tab on the ribbon and click Filter. This adds dropdown arrows to each header cell.
- Click the dropdown arrow in the column you want to count by (e.g., "Status") and uncheck "Select All", then check only the values you want to include (e.g., "Completed" and "Pending"). Click OK. Excel will now hide rows that don’t match your criteria.
- Click on an empty cell where you want the count to appear. This will be your result cell.
- Type the formula:
=SUBTOTAL(103, range). Replacerangewith the actual cell range of the column you're counting. For example, if your "Status" column is in column B from row 2 to row 100, use=SUBTOTAL(103, B2:B100). The number 103 tells SUBTOTAL to count visible cells only, ignoring hidden rows. - Now wrap that SUBTOTAL inside a COUNTIF function. Change your formula to:
=COUNTIF(range, criteria) * (SUBTOTAL(103, range) > 0). This is a workaround since COUNTIF doesn’t natively work with SUBTOTAL. For example:=COUNTIF(B2:B100, "Completed") * (SUBTOTAL(103, B2:B100) > 0). This multiplies the total count by 1 if any row is visible, or 0 if all are hidden. - Alternatively, use a more robust formula:
=SUMPRODUCT(SUBTOTAL(103, OFFSET(B2:B100, ROW(B2:B100)-ROW(B2), 0, 1)), --(B2:B100="Completed")). This version dynamically checks each row’s visibility and only counts matches that are visible. - Press Enter. Excel now shows the count of visible rows matching your criteria. Test it: apply a different filter (e.g., filter for "Region = North") and watch the number update automatically.
Common Mistakes
- Using SUBTOTAL function 3 (COUNT) instead of 103. Function 3 counts all cells, including hidden ones. Always use 103 for visible-only counting.
- Forgetting to apply a filter before testing. The SUBTOTAL(103) function only ignores rows if Excel’s AutoFilter is active. If no filter is applied, it behaves like a regular COUNT.
- Using absolute references incorrectly. If you copy the formula to another cell, make sure your range (e.g., B2:B100) still points to the correct column. Use relative references unless you specifically need fixed ranges.
- Assuming COUNTIF works natively with SUBTOTAL. COUNTIF does not adapt to hidden rows. You must combine it with SUBTOTAL or use SUMPRODUCT for true dynamic results.
- Including blank rows or merged cells in the range. These can cause SUBTOTAL to return 0 or incorrect values. Clean your data first.
Tips
- Use structured references if your data is in an Excel Table (Ctrl+T). For example:
=SUMPRODUCT(SUBTOTAL(103, OFFSET([Status], ROW([Status])-ROW([Status]@), 0, 1)), --([Status]="Completed")). This automatically adjusts when rows are added or removed. - Label your result cell clearly (e.g., "Visible Completed Tasks:") to avoid confusion when sharing the sheet.
- Use conditional formatting on the result cell to highlight when the count drops below a threshold (e.g., red if less than 5).
- Test your formula by hiding rows manually (right-click row number > Hide) and see if the count updates. This confirms SUBTOTAL(103) is working correctly.
- If you’re counting text values, ensure there are no extra spaces. Use TRIM in a helper column if needed:
=TRIM(B2)before applying filters. - For multiple criteria, nest additional SUMPRODUCT conditions. Example: count visible rows where Status="Completed" AND Region="East":
=SUMPRODUCT(SUBTOTAL(103, OFFSET(B2:B100, ROW(B2:B100)-ROW(B2), 0, 1)), --(B2:B100="Completed"), --(C2:C100="East"))
Troubleshooting
- Count stays at zero even when data is visible: Check if the filter is applied correctly. Sometimes filters show as active but don’t match any rows. Reapply the filter or clear it and rebuild.
- Formula returns #VALUE!: You likely included text in a numeric range or referenced a merged cell. Ensure your range contains only the column you’re evaluating. Remove merged cells or use helper columns.
- FILTER doesn’t appear on the Data tab: Your Excel version may not support it. Update Excel or use the classic Filter button (Data > Filter). If unavailable, you may be using Excel Online or a mobile app—these have limited functionality.
- The formula works but doesn’t update when filtering: Check Calculation Options (Formulas > Calculation Options). If set to Manual, press F9 to recalculate. Switch to Automatic to prevent this.
- Subtotal returns 0 even with visible rows: Verify the column you’re counting doesn’t contain errors (like #N/A). Use IFERROR in a helper column:
=IFERROR(B2, "")before filtering.
Frequently Asked Questions
Can I use SUBTOTAL with COUNTIF for multiple columns at once?
No. SUBTOTAL operates on one range at a time. To count across multiple columns, apply separate formulas for each, or use a helper column to combine criteria into one field first.Does this work with Excel Online or mobile apps?
Limited. Excel Online supports basic SUBTOTAL and filters, but complex formulas like SUMPRODUCT with OFFSET may not calculate correctly. Use the desktop app for full functionality.Why not just use COUNTIFS instead of SUBTOTAL with COUNTIF?
COUNTIFS counts all matching rows, including hidden ones. It ignores filtering. SUBTOTAL with SUMPRODUCT is the only reliable way to respect visible rows during filtering.Can I use this with pivot tables?
No. Pivot tables have their own filtering and summarization. Use the built-in value field settings (e.g., Count of Status) instead of SUBTOTAL formulas.What if my data has blank cells in the counting column?
Blank cells are ignored by COUNTIF and SUBTOTAL(103). If you need to count blanks as well, use COUNTBLANK in a separate formula or include "" in your criteria:=COUNTIF(range, "").
Is there a faster alternative than SUMPRODUCT with OFFSET for large datasets?
Yes. Convert your data to an Excel Table (Ctrl+T), then use structured references with SUBTOTAL. This avoids volatile OFFSET and recalculates faster on large sheets. Example:=SUMPRODUCT(SUBTOTAL(103, OFFSET([Status], ROW([Status])-ROW([Status]@), 0, 1)), --([Status]="Completed")).