Combine Subtotal and COUNTIF in Excel

Formulas & functions · Updated November 22, 2026

Yes, you can combine SUBTOTAL and COUNTIF in Excel, but not directly inside a single formula. The standard approach is to use SUMPRODUCT with SUBTOTAL’s function number 3 (COUNTA) and a range comparison, or to use a helper column with a simple COUNTIF and then SUBTOTAL on that helper column. The most flexible method that works in all Excel versions is the SUMPRODUCT formula: =SUMPRODUCT(SUBTOTAL(3,OFFSET(range,ROW(range)-MIN(ROW(range)),0,1)),--(range="criteria")).

Why You Can’t Just Nest COUNTIF Inside SUBTOTAL

Excel’s SUBTOTAL function is designed to ignore rows hidden by filtering, but it only accepts a limited set of operations: AVERAGE, COUNT, COUNTA, MAX, MIN, PRODUCT, STDEV, SUM, and VAR. COUNTIF is not one of them. If you try =SUBTOTAL(9,COUNTIF(A2:A100,"Yes")), Excel returns a #VALUE! error because COUNTIF returns an array, not a single value that SUBTOTAL can aggregate. The workaround is to force SUBTOTAL to evaluate each cell individually, which is exactly what the OFFSET-based array formula does.

How the SUMPRODUCT + SUBTOTAL Formula Works

Assume you have a list of sales regions in A2:A20 and a status column B2:B20 with values like “Closed” or “Open”. You filter the data to see only certain regions, and you want to count how many “Closed” rows are visible. The formula below does that:

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

Here’s the breakdown:

  • ROW(B2:B20)-MIN(ROW(B2:B20)) returns an array of offsets: 0,1,2,…,18.
  • OFFSET(B2:B20, offset, 0, 1) creates a reference to each individual cell in B2:B20, one at a time.
  • SUBTOTAL(3, ...) uses function number 3 (COUNTA) on each single-cell reference. COUNTA returns 1 if the cell is visible and non-empty, 0 if hidden or empty. Because SUBTOTAL is evaluated for each offset, it respects the filter on each row.
  • --(B2:B20="Closed") converts TRUE/FALSE to 1/0.
  • SUMPRODUCT multiplies the two arrays element-wise and sums the results, giving the count of visible rows where B equals “Closed”.

For the example with 5 visible rows, 3 of which are “Closed”, the formula returns 3. Note: this is an array formula, but SUMPRODUCT handles arrays natively, so you don’t need Ctrl+Shift+Enter in modern Excel. In older Excel (pre-365), you may need to enter it as an array formula with Ctrl+Shift+Enter.

The Helper Column Alternative (Simpler for Beginners)

If the OFFSET formula feels too complex, use a helper column. In column C, enter =COUNTIF(B2,"Closed") for each row (or simply =IF(B2="Closed",1,0)). Then use =SUBTOTAL(9,C2:C20) to sum the visible helper values. This gives the same result and is easier to audit. The downside is an extra column, but it’s often the most practical solution in shared workbooks.

Using SUBTOTAL with COUNTIFS (Multiple Criteria)

To count visible rows that meet multiple conditions, adapt the SUMPRODUCT formula by adding more comparison arrays. For example, to count visible rows where B2:B20 is “Closed” and C2:C20 is “East”:

=SUMPRODUCT(SUBTOTAL(3,OFFSET(B2:B20,ROW(B2:B20)-MIN(ROW(B2:B20)),0,1)),--(B2:B20="Closed"),--(C2:C20="East"))

This works because SUMPRODUCT multiplies all arrays. The SUBTOTAL array still checks visibility for each row, and the other arrays check the criteria. For Excel 365, you could also use FILTER and COUNT, but that requires dynamic arrays: =COUNTA(FILTER(B2:B20,(B2:B20="Closed")*(C2:C20="East"))) – however, FILTER ignores hidden rows only if you combine it with SUBTOTAL, which is not straightforward. The OFFSET method remains the most compatible.

Common Problems and How to Fix Them

  • #VALUE! error: Usually because the OFFSET range is not a single column or the ROW function returns an array that doesn’t match. Ensure the ranges are the same size and start on the same row.
  • Wrong count when rows are hidden manually: SUBTOTAL only ignores rows hidden by AutoFilter (filter arrows), not rows hidden via the Hide Rows command. If you need to ignore manually hidden rows, you’ll need a different approach, such as using CELL(“width”,A1) to detect hidden columns, but that’s unreliable. For manual row hiding, consider using a VBA macro or the AGGREGATE function with option 7, but AGGREGATE also only respects filtered rows, not manually hidden ones.
  • Performance issues on large datasets: The OFFSET formula is volatile and recalculates every time any cell changes. For 10,000+ rows, it can slow down your workbook. Use the helper column method for large data.
  • Counting text vs numbers: SUBTOTAL(3, …) counts non-empty cells, including text. If you need to count only numbers, use function number 2 (COUNT) instead of 3.

Comparing SUBTOTAL+COUNTIF with AGGREGATE

Excel’s AGGREGATE function (available since Excel 2010) can also ignore hidden rows, but it doesn’t support COUNTIF directly either. You can use AGGREGATE with function 15 (SUM) and option 7 (ignore hidden rows) on an array of 1s and 0s, but you still need to generate that array with a formula. For example: =SUMPRODUCT(AGGREGATE(15,7,ROW(B2:B20)/(B2:B20="Closed"),1)) is not correct because AGGREGATE returns a single value. The SUMPRODUCT+SUBTOTAL method is more straightforward. AGGREGATE is better for ignoring error values, but for counting with criteria, SUBTOTAL+SUMPRODUCT is the standard.

Frequently Asked Questions

Can I use COUNTIF directly on a filtered range?

No, COUNTIF ignores filters and counts all rows in the range, including hidden ones. That’s why you need SUBTOTAL or a helper column.

Does SUBTOTAL work with COUNTIFS for multiple conditions?

You can’t nest COUNTIFS inside SUBTOTAL, but you can extend the SUMPRODUCT formula with additional criteria arrays, as shown above. Each additional condition adds another --(range=criteria) term.

Why does my formula return 0 when there are visible matches?

Check that your range references are correct and that the criteria string matches exactly, including case (COUNTIF is case-insensitive, but SUBTOTAL+SUMPRODUCT with -- is also case-insensitive). Also ensure the OFFSET range starts at the same row as your data. If you have headers in row 1, start at row 2.

Is there a way to do this without a helper column in Excel 365?

Yes, you can use the new TAKE and FILTER functions, but they don’t respect filters directly. The most reliable dynamic array method is to use =ROWS(FILTER(B2:B20,(B2:B20="Closed")*(SUBTOTAL(3,OFFSET(B2:B20,ROW(B2:B20)-MIN(ROW(B2:B20)),0,1))>0))) – but that’s more complex. The OFFSET method remains the simplest cross-version solution.

Can I use SUBTOTAL with COUNTIF in Google Sheets?

Google Sheets has a similar issue. You can use =SUMPRODUCT(SUBTOTAL(3,OFFSET(B2:B20,ROW(B2:B20)-MIN(ROW(B2:B20)),0,1)),--(B2:B20="Closed")) – it works the same way. Google Sheets also has a FILTER function that respects filters when combined with SUBTOTAL in a different way, but the OFFSET method is portable.

Does this work with Excel tables (ListObjects)?

Yes, but you need to adjust the range references. If your data is in a table named Table1, use =SUMPRODUCT(SUBTOTAL(3,OFFSET(Table1[Status],ROW(Table1[Status])-MIN(ROW(Table1[Status])),0,1)),--(Table1[Status]="Closed")). Note that table column references are not volatile, but the OFFSET still is.

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.