Table of Contents
The COUNTIF and COUNTIFS functions in VBA are used to count the number of cells in a range that meet a specific criteria. To write these functions, first specify the range of cells to be counted, followed by the criteria to be met. The COUNTIF function is used for a single criteria, while the COUNTIFS function allows for multiple criteria to be specified. Both functions can be written using the “Application.WorksheetFunction” command in VBA. The syntax for the COUNTIF function is “=Application.WorksheetFunction.CountIf(range, criteria)”, while the syntax for the COUNTIFS function is “=Application.WorksheetFunction.CountIfs(range1, criteria1, range2, criteria2, … )”. These functions are useful for analyzing and manipulating data in VBA.
VBA: Write COUNTIF and COUNTIFS Functions
You can use the following methods to write COUNTIF and COUNTIFS functions using VBA in Excel:
Method 1: COUNTIF Function in VBA
Sub Countif_Function()
Range("E2") = WorksheetFunction.Countif(Range("B2:B12"), ">20")
End SubThis particular example will count the number of values in the range B2:B12 that are greater than 20 and assign the result to cell E2.
Method 2: COUNTIFS Function in VBA
Sub Countifs_Function()
Range("E2") = WorksheetFunction.CountIfs(Range("A2:A12"), "Mavs", Range("B2:B12"), ">20")
End SubThis particular example will count the number of rows where the value in the range A2:A12 is equal to “Mavs” and the value in the range B2:B12 is greater than 20 and then assign the result to cell E2.
The following examples shows how to use each of these methods in practice with the following dataset in Excel that contains information about various basketball players:

Example 1: COUNTIF Function in VBA
Suppose we would like to count the number of values in the points column that are greater than 20.
We can create the following macro to perform this COUNTIF function:
Sub Countif_Function()
Range("E2") = WorksheetFunction.Countif(Range("B2:B12"), ">20")
End SubWhen we run this macro, we receive the following output:

Notice that cell E2 contains a value of 6.
This tells us that there are 6 values in the points column that are greater than 20.
Example 2: COUNTIFS Function in VBA
Suppose we would like to count the number of rows that meet the following criteria:
- Player is on the Mavs team.
- Player scored more than 20 points.
We can create the following macro to perform this COUNTIFS function:
Sub Countifs_Function()
Range("E2") = WorksheetFunction.CountIfs(Range("A2:A12"), "Mavs", Range("B2:B12"), ">20")
End SubWhen we run this macro, we receive the following output:

Notice that cell E2 contains a value of 2.
This tells us that there are two rows where the player is on the Mavs team and the player scored more than 20 points.
Note: In this example, we created a COUNTIFS function using two criteria ranges but you can use as many criteria ranges as you’d like within the WorksheetFunction.CountIfs method.
Cite this article
stats writer (2024). How do you write the COUNTIF and COUNTIFS functions in VBA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-write-the-countif-and-countifs-functions-in-vba/
stats writer. "How do you write the COUNTIF and COUNTIFS functions in VBA?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-you-write-the-countif-and-countifs-functions-in-vba/.
stats writer. "How do you write the COUNTIF and COUNTIFS functions in VBA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-write-the-countif-and-countifs-functions-in-vba/.
stats writer (2024) 'How do you write the COUNTIF and COUNTIFS functions in VBA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-write-the-countif-and-countifs-functions-in-vba/.
[1] stats writer, "How do you write the COUNTIF and COUNTIFS functions in VBA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How do you write the COUNTIF and COUNTIFS functions in VBA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
