COUNTIFS in VBA: How to Use It Correctly

VBA & macros · Updated November 23, 2026

The direct answer: you can use Excel’s COUNTIFS function inside VBA by calling Application.WorksheetFunction.CountIfs, or by using the Evaluate method with a formula string. The WorksheetFunction approach is cleaner for most tasks, but it throws an error if no cells match, so you need to handle that case. Here’s the simplest working example: result = Application.WorksheetFunction.CountIfs(Range("A1:A100"), ">10", Range("B1:B100"), "Yes").

Why Use COUNTIFS in VBA at All

COUNTIFS is a worksheet function that counts cells meeting multiple criteria across one or more ranges. In VBA, you might need this count to drive logic—for example, to check if a category has enough entries before running a report, to validate data, or to build a summary table. Doing it in VBA avoids writing formulas into cells and recalculating, which is faster and keeps your sheet clean. The function works the same way as in the worksheet: each range must have the same number of rows and columns, and you pair each range with a criterion.

How to Call COUNTIFS from VBA

There are two main ways to call COUNTIFS in VBA. The first is the WorksheetFunction method, which is typed and gives you IntelliSense. The second is Evaluate, which lets you pass a formula string and is more flexible for dynamic criteria.

Here is a complete macro using WorksheetFunction:

Sub CountExample()
    Dim ws As Worksheet
    Dim result As Long
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    ' Count rows where A > 10 and B = "Yes"
    On Error Resume Next
    result = Application.WorksheetFunction.CountIfs( _
        ws.Range("A1:A100"), ">10", _
        ws.Range("B1:B100"), "Yes")
    If Err.Number <> 0 Then
        result = 0
        Err.Clear
    End If
    On Error GoTo 0
    
    MsgBox "Count: " & result
End Sub

Note the On Error Resume Next block: if no cells match, COUNTIFS returns a #VALUE! error, and VBA raises an error. Setting result to 0 is a safe fallback. The Evaluate method avoids the error handling because it returns an error value instead of raising an exception:

Sub CountWithEvaluate()
    Dim result As Variant
    result = Evaluate("COUNTIFS(A1:A100,">10",B1:B100,"Yes")")
    If IsError(result) Then result = 0
    MsgBox "Count: " & result
End Sub

Both return the same count. Use WorksheetFunction when you want compile-time checking; use Evaluate when you need to build the formula dynamically, like with variable criteria.

Criteria Syntax and Wildcards

Criteria in COUNTIFS are text strings, so you must enclose them in quotes inside the VBA code. For numeric comparisons, use “>10”, “<5", "=0". For text, use "Yes" or "=Yes" (the equals sign is optional). Wildcards work too: "A*" matches cells starting with A, "*A" ends with A, and "?A" matches any single character followed by A. To match a literal asterisk or question mark, use a tilde: "~*" matches an asterisk. For dates, you can use a date serial number or a string that Excel interprets, but it's safer to use CDate or a numeric value. For example, to count dates after January 1, 2024, use ">" & DateSerial(2024, 1, 1).

Common Mistakes and How to Fix Them

One frequent error is mismatched range sizes. If you pass Range("A1:A10") and Range("B1:B20"), VBA will raise a runtime error. Always ensure each range has the same number of rows and columns. Another mistake is forgetting that criteria are strings: writing CountIfs(rng, >10) is invalid; you need ">10". Also, when using cell references inside criteria, you must concatenate: ">" & Range("C1").Value. A third issue is using COUNTIFS with entire columns (e.g., Range("A:A"))—this works but can slow down your macro if used repeatedly; better to limit to the used range. Finally, remember that COUNTIFS is case-insensitive by default. If you need case-sensitive matching, you must use a different approach, like an array formula with EXACT, or a loop with StrComp.

COUNTIFS vs. SUMPRODUCT vs. AutoFilter

COUNTIFS is the simplest for counting with multiple criteria, but it has limitations: it cannot count based on the result of a calculation on the cell (like counting cells where the length is greater than 5), and it cannot handle OR logic easily. For those cases, you can use SUMPRODUCT in VBA via Evaluate. For example, to count rows where A is “Red” OR “Blue”, you could use Evaluate("SUMPRODUCT(--((A1:A100="Red")+(A1:A100="Blue")>0))"). Another alternative is using AutoFilter and the Subtotal function, but that’s more invasive. In practice, if your criteria are simple and you just need a count, COUNTIFS is the best choice. If you need to count based on a formula result, use SUMPRODUCT or a loop with If statements.

Performance Considerations

When using COUNTIFS in VBA, the function is fast for moderate data sizes (up to tens of thousands of rows). However, calling it in a loop for each row can be slow because each call recalculates. If you need to count for many different criteria, consider building a dictionary or using a scripting dictionary to accumulate counts in one pass. Also, if your data is large, avoid using entire column references like Range("A:A"); instead, find the last used row with Cells(Rows.Count, 1).End(xlUp).Row and use that. Another tip: turn off screen updating and calculation while running the macro, then restore them. Use Application.ScreenUpdating = False and Application.Calculation = xlCalculationManual at the start, and set them back at the end.

Frequently Asked Questions

Can I use COUNTIFS in VBA without WorksheetFunction? Yes, you can use Evaluate as shown above, or you can write a custom loop. But WorksheetFunction is the most straightforward.

Why does my COUNTIFS return a #VALUE! error? This usually happens when the ranges are not the same size, or when a criterion is invalid (e.g., a malformed date). Check that each range has the same number of rows and columns, and that criteria strings are properly formatted.

How do I count with OR logic in COUNTIFS? COUNTIFS uses AND logic between criteria pairs. For OR logic, you can add multiple COUNTIFS together (e.g., CountIfs(rng1, "A", rng2, "X") + CountIfs(rng1, "B", rng2, "Y")), or use SUMPRODUCT with an array formula.

Is COUNTIFS case-sensitive? No, by default it is not. If you need case-sensitive counting, you can use an array formula with EXACT, or loop through cells with StrComp and vbBinaryCompare.

Can I use COUNTIFS with a dynamic range? Yes, you can use Range("A1:A" & lastRow) where lastRow is computed. Or use a named range or a ListObject (Excel table) reference, such as ws.ListObjects("Table1").Range.

Does COUNTIFS work with errors in cells? COUNTIFS ignores error values in the range unless the criterion specifically matches them. For example, counting cells that contain #N/A requires a criterion like "#N/A" but that’s tricky; it’s easier to use ISERROR with SUMPRODUCT.

Safety and Best Practices

Before running any macro that modifies data or writes to cells, always save a copy of your workbook. Macros cannot be undone with Ctrl+Z, so if something goes wrong, you’ll lose changes. Also, test your macro on a small sample range first. When using WorksheetFunction.CountIfs, remember that it can raise an error if no matches are found; always handle that with error handling. Finally, if you are working with dates, be aware of regional date settings—use DateSerial to avoid ambiguity. For example, ">" & DateSerial(2024, 1, 1) is safer than ">1/1/2024" because the latter may be interpreted as January 1 or January 12 depending on your system.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

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

Spotted an error in this guide? Let us know.