30 Advanced Excel Tips for 2026

Excel Champs / VBA / Using the COUNTIF Function in VBA in Excel (Code)
By Puneet Gogia (Microsoft MVP)

Using the COUNTIF Function in VBA in Excel (Code)

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Range("B1").Value = WorksheetFunction.CountIf(Range("A2:A11"), ">5000") counts the cells in A2:A11 above 5,000. The criteria is text in quotes, as in a worksheet formula: "East" for an exact match, "*North*" for cells containing North, and "<>" for cells that aren’t empty.

To use a variable or a cell value, join the operator to it with &: WorksheetFunction.CountIf(Range("A2:A11"), ">=" & Range("D1").Value). Writing ">=limit" inside the quotes compares against the word “limit” and finds nothing in a column of numbers, without an error.

For more than one condition, use CountIfs with pairs of range and criteria: WorksheetFunction.CountIfs(Range("A2:A11"), ">5000", Range("B2:B11"), "East"). Every range must be the same size.

Numbers stored as text aren’t counted by a numeric condition such as ">5000", so a count that looks too low usually means imported text. Convert the column with Data → Text to Columns → Finish before running the macro.

If you want to use the COUNTIF function in a VBA code, you need to use the WorksheetFunction property, which allows you to access all the Excel functions within a VBA code.

countif-function-in-vba

In this tutorial, you will learn to write and use COUNTIF in a code. For this, we have a few values in column A, and we need to count the values above 5000.

Write a VBA Code to use COUNTIF

Once you open the Visual Basic Editor from the developer tab, open a new module and create a new macro.

  1. First, type “Application” and then enter a dot (.) to get the list of methods and properties.
  2. Now, select “WorksheetFunction” from the list or type it using the keyboard.
  3. Again, type a dot (.) to get the list of the functions, and from there, select COUNTIF.
  4. Next, enter the starting parentheses to start the function. In the function’s first argument, specify the range where you want to count the values.
  5. From here, in the second argument, enter the greater than operator in double quotation marks, and join it with 5000 using an ampersand (&) to use it as the criteria.
  6. Last, specify the cell where you want to get the count (cell B1 here).
Sub COUNTIF_VBA()
Range("B1") = Application.WorksheetFunction.CountIf(Range("A2:A11"), ">" & 5000)
End Sub
vba-code-to-use-countif

The above example shows that when you run the code, it returns the count in cell B1.

Important Note

When you use an Excel worksheet function in a VBA code using WorksheetFunction, it won’t show you the real names of the arguments. You only see generic names like Arg1 and Arg2. So, you need to know what each argument does before you write a code for the function.

worksheet-function-in-vba-code

In the above example, you can see that the real names of the arguments are not there, but if you use COUNTIF in a worksheet, it shows you the names of the arguments (range and criteria).

countif-without-agruments

If you want to get the result of COUNTIF in a message box, you can use the code below:

Sub COUNTIF_VBA()
MsgBox Application.WorksheetFunction.CountIf(Range("A2:A11"), ">" & 5000)
End Sub

The result of COUNTIF in VBA is a number with the Double data type. If you want to use the count later in your code, you can store it in a variable first:

Sub COUNTIF_Variable()
Dim myCount As Double
myCount = Application.WorksheetFunction.CountIf(Range("A2:A11"), ">" & 5000)
MsgBox "There are " & myCount & " values above 5000."
End Sub

Use a Cell Value as the Criteria

Instead of typing 5000 in the code, you can take the criteria from a cell. This way, you can change the value in the cell and run the code again without editing it. In the code below, the value comes from cell D1:

Sub COUNTIF_Cell_Criteria()
Range("B1") = Application.WorksheetFunction.CountIf(Range("A2:A11"), ">" & Range("D1").Value)
End Sub

Keep the operator inside the double quotation marks and the cell reference outside them. If you write “>D1”, COUNTIF looks for values greater than the text “D1” and not the value in cell D1.

Count Text Values with Wildcards

COUNTIF in VBA works with text criteria and wildcard characters, just like in a worksheet. An asterisk (*) matches any number of characters, and a question mark (?) matches a single character. The code below counts all the cells that have the word “Excel” anywhere in them:

Sub COUNTIF_Wildcard()
Range("B1") = Application.WorksheetFunction.CountIf(Range("A2:A11"), "*Excel*")
End Sub

COUNTIF is not case-sensitive, so “*Excel*” also counts “excel” and “EXCEL”. If you want to count an actual asterisk or question mark, type a tilde (~) before it, like “~*”.

Use COUNTIFS for More Than One Condition

When you need to count with two or more conditions, use CountIfs from WorksheetFunction. You enter the arguments in pairs: a range and then the criteria for that range. In the code below, it counts the values above 5000 in column A where column C has “North”:

Sub COUNTIFS_VBA()
Range("B1") = Application.WorksheetFunction.CountIfs(Range("A2:A11"), ">" & 5000, Range("C2:C11"), "North")
End Sub

All the ranges you use in CountIfs must have the same number of rows and columns.

Count in a Dynamic Range

If the number of values in column A changes, a fixed range like A2:A11 will miss the new values. In the code below, I find the last used row in column A first and then use it in the range:

Sub COUNTIF_Dynamic_Range()
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("B1") = Application.WorksheetFunction.CountIf(Range("A2:A" & lastRow), ">" & 5000)
End Sub

Enter COUNTIF as a Formula with VBA

All the codes above put a fixed number in the cell. If the values in column A change later, the count won’t update until you run the code again. If you want a count that updates on its own, use the code to enter the COUNTIF formula in the cell:

Sub COUNTIF_Formula()
Range("B1").Formula = "=COUNTIF(A2:A11,"">5000"")"
End Sub

Inside a VBA string, you need to double the quotation marks around the criteria (“”>5000″”). So, use WorksheetFunction when you only need the result, and the Formula property when you want the cell to keep the formula.

Common Mistakes with COUNTIF in VBA

  • Operator outside the quotes: The operator must be text, like “>” & 5000 or “>5000”. Without the quotation marks, the code won’t run.
  • Numbers stored as text: A criteria like “>5000” doesn’t count numbers that are stored as text in the cells. Convert them to numbers first.
  • Wrong sheet: Range(“A2:A11”) refers to the active sheet. To count on a specific sheet, use Worksheets(“Sheet1”).Range(“A2:A11”) in the first argument.
  • Long text criteria: COUNTIF returns wrong results when you use it to match text longer than 255 characters.

Frequently Asked Questions

Does COUNTIF in VBA update when the data changes?

No. WorksheetFunction.CountIf returns a fixed value, so you need to run the code again to get a new count. If you want the count to update on its own, enter the COUNTIF formula in the cell with the Formula property.

How do I use COUNTIF on another sheet in VBA?

Refer to the range with the sheet name in front of it, like in the code below. It counts the values in Sheet2 and shows the result in the active sheet.

Sub COUNTIF_Other_Sheet()
Range("B1") = Application.WorksheetFunction.CountIf(Worksheets("Sheet2").Range("A2:A11"), ">" & 5000)
End Sub

How do I count blank cells in VBA?

Use CountBlank from WorksheetFunction, like Application.WorksheetFunction.CountBlank(Range(“A2:A11”)). A cell with only a space is not blank, so it isn’t counted.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel

Leave a Comment