Subtotal and COUNTIF Combined in Excel

Formulas & functions · Updated November 22, 2026

Yes, you can combine SUBTOTAL and COUNTIF in Excel, but not directly in a single formula. The standard approach is to use SUMPRODUCT with SUBTOTAL’s function number 3 (COUNTA) or 2 (COUNT) and a range comparison, or to use a helper column with a regular COUNTIF that references visible rows only. The most reliable formula for counting visible cells that meet a condition is =SUMPRODUCT(SUBTOTAL(3,OFFSET(range,ROW(range)-MIN(ROW(range)),0,1)),--(range=criteria)).

Why SUBTOTAL and COUNTIF Don’t Work Together Directly

COUNTIF ignores hidden rows entirely—it counts all cells in the range regardless of whether they are filtered out. SUBTOTAL, on the other hand, is designed to respect filtering, but it only performs aggregate functions like SUM, AVERAGE, or COUNTA; it has no built-in condition argument. So a formula like =SUBTOTAL(3,COUNTIF(A2:A20,"Yes")) will not work because COUNTIF returns a single number, and SUBTOTAL treats that as one value.

The workaround is to make SUBTOTAL evaluate each row individually, then multiply that result by a condition check. That is exactly what the OFFSET-based SUMPRODUCT formula does: it creates a separate SUBTOTAL for each cell in the range, each returning 1 if the row is visible and 0 if hidden, then multiplies by 1 if the cell matches the criteria and 0 otherwise. Summing those products gives the count of visible matching cells.

The Exact Formula and How It Works

Assume your data has names in A2:A20 and a status column B2:B20, and you want to count visible rows where B equals “Complete”. With the range B2:B20, the formula is:

=SUMPRODUCT(SUBTOTAL(3,OFFSET(B2,ROW(B2:B20)-MIN(ROW(B2:B20)),0,1)),--(B2:B20="Complete"))

Here’s the breakdown:

  • ROW(B2:B20)-MIN(ROW(B2:B20)) returns an array of offsets from the first cell: 0,1,2,…18.
  • OFFSET(B2, offset, 0, 1) creates a one-cell range for each row.
  • SUBTOTAL(3, ...) uses function number 3 (COUNTA) which returns 1 for a visible non-empty cell and 0 for a hidden row. (Function number 2, COUNT, would ignore text, so use 3 for text criteria.)
  • --(B2:B20="Complete") converts TRUE/FALSE to 1/0.
  • SUMPRODUCT multiplies the two arrays element-wise and sums the results.

If you have 5 visible rows with “Complete” out of 10 total, the formula returns 5. If you manually hide rows (right-click > Hide), SUBTOTAL with function 3 also treats those as hidden, so the formula works for manual hiding as well as auto-filtering.

Using a Helper Column for Simplicity

Many users prefer a helper column because it is easier to read and debug. In column C, enter a formula that checks the condition and the visibility:

=IF(SUBTOTAL(3,A2),IF(B2="Complete",1,0),0)

Drag down for all rows. Then use a simple =SUM(C2:C20) to get the count of visible matching rows. This works because SUBTOTAL(3,A2) returns 1 if row 2 is visible, and the nested IF checks the condition. The helper column updates automatically when you filter or hide rows.

This approach is less elegant but more transparent, especially if you need to reuse the count in multiple formulas or pivot tables.

Counting Unique Visible Values with SUMPRODUCT

A common variation is counting unique visible items that meet a condition. For example, count distinct customers in column A who have status “Paid” in column B. The formula becomes more complex:

=SUMPRODUCT((SUBTOTAL(3,OFFSET(A2,ROW(A2:A20)-MIN(ROW(A2:A20)),0,1)))*(B2:B20="Paid")*(COUNTIFS(A2:A20,A2:A20,B2:B20,"Paid")>0))

But this formula is flawed because COUNTIFS counts all rows, not just visible ones. A better approach is to use a helper column with a formula that marks each row as 1 if it is the first visible occurrence of that customer with “Paid”, then sum that column. For row 2, assuming data starts at row 2:

=IF(SUBTOTAL(3,A2),IF(B2="Paid",IF(COUNTIFS(A$2:A2,A2,B$2:B2,"Paid")=1,1,0),0),0)

This counts each customer only once, but only if the first occurrence is visible. If the first occurrence is hidden but a later one is visible, it won’t count. For a fully accurate unique visible count, you would need an array formula with AGGREGATE or a more advanced setup. In practice, most users use a pivot table or Power Query for unique visible counts.

Common Errors and How to Fix Them

Several mistakes trip up users when combining these functions:

  • Using function number 2 instead of 3: Function 2 (COUNT) ignores text, so if your criteria column contains text, SUBTOTAL returns 0 for every row. Use 3 (COUNTA).
  • Forgetting to press Ctrl+Shift+Enter: In older Excel versions, SUMPRODUCT doesn’t require array entry, but if you use a version before Excel 365 and you use an array formula with IF, you may need to confirm with Ctrl+Shift+Enter. In Excel 365, dynamic arrays handle it automatically.
  • OFFSET is volatile: OFFSET recalculates every time any cell changes, which can slow down large workbooks. For thousands of rows, consider using a helper column or AGGREGATE instead.
  • Hidden rows vs. filtered rows: SUBTOTAL with function 3 treats manually hidden rows as hidden, but only if the hidden rows are within a filtered range? Actually, SUBTOTAL(3) ignores rows hidden by filtering and also rows hidden manually (right-click > Hide), but it does not ignore rows hidden by grouping (outline). To ignore grouped rows, you need function 103 (COUNTA) which works only with the SUBTOTAL function when used in a filtered list? Wait, function 103 ignores manually hidden rows but not filtered rows? Let me clarify: In Excel, function numbers 1-11 ignore manually hidden rows but not filtered rows? Actually, the correct behavior: Function numbers 1-11 ignore rows hidden by filtering (auto-filter) but do NOT ignore rows hidden manually (right-click > Hide). Function numbers 101-111 ignore rows hidden manually but do NOT ignore filtered rows. So for auto-filter, use 3; for manual hide, use 103. The formula above uses 3, which works with auto-filter but not manual hide. To cover both, you can use 103 in the SUBTOTAL inside the OFFSET formula, but then it will not respond to auto-filter. There is no single function that ignores both. Most users rely on auto-filter, so 3 is standard.

Alternatives: AGGREGATE and Excel 365 Dynamic Arrays

If you have Excel 365, you can use the FILTER function to get visible rows, then COUNTIF on the result. However, FILTER does not directly respect hidden rows—it only filters based on criteria. To get visible rows, you need to combine FILTER with a visibility check, which again requires SUBTOTAL and OFFSET. A cleaner alternative is to use the AGGREGATE function with function number 3 (COUNTA) and option 7 (ignore hidden rows), but AGGREGATE does not accept a criteria argument either. So the SUMPRODUCT approach remains the most flexible.

Another alternative is to use a Pivot Table. Pivot tables automatically ignore hidden rows in the source data when you refresh, and you can add a calculated field with a COUNTIF-like formula using GETPIVOTDATA. This is often the best choice for large datasets.

Frequently Asked Questions

Can I use COUNTIFS with SUBTOTAL? No, COUNTIFS does not have a visibility parameter. You must use the SUMPRODUCT+SUBTOTAL pattern or a helper column.

Why does my formula return 0 when I filter? Check that you used function number 3, not 2, and that your criteria range matches the OFFSET range. Also ensure you entered the formula as a regular formula (SUMPRODUCT doesn’t need array entry).

Does this work with text criteria? Yes, use function 3 (COUNTA) and the comparison like "Complete". For numeric criteria, you can use function 2, but 3 works for numbers too as long as cells are not empty.

How do I count visible cells that are not blank? Simply use =SUBTOTAL(3,A2:A20) which counts all non-empty visible cells. No need for COUNTIF.

Can I use this in Google Sheets? Yes, but the OFFSET syntax works the same. However, Google Sheets has a FILTER function that can be combined with SUBTOTAL? Actually, Google Sheets does not have SUBTOTAL with OFFSET in the same way? It does have SUBTOTAL, and the same formula works, but you may need to use ARRAYFORMULA. A simpler way in Sheets is to use the FILTER function with a helper column that checks visibility using SUBTOTAL(103, …).

Is there a non-volatile alternative to OFFSET? Yes, you can use the INDEX function to return a reference, but INDEX is not volatile. A formula like =SUMPRODUCT(SUBTOTAL(3,INDEX(B2:B20,ROW(B2:B20)-MIN(ROW(B2:B20))+1,1)),--(B2:B20="Complete")) works and is less volatile. However, INDEX in this context still returns a reference, and SUBTOTAL evaluates each one. This is a good alternative for large datasets.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.