Table Of Contents
Example of Excel VBA Countif Function
Let us see a simple example.
Look at the below example of counting values from the lot.
The above image shows city names from cells A1 to A10. So, for example, in cell C3, we need to count how many times the city name "Bangalore" appears in the range A1 to A10.
Follow the below steps to write the code to apply the COUNTIF function.
Step 1: Start the Sub procedure.
Code:
Option Explicit Sub Countif_Example1() End Sub
Step 2: Since we need to store the result in cell C3, start the Range("C3").Value.
Code:
Sub Countif_Example1() Range("C3").Value = End Sub
Step 3: In cell C3, by applying the Excel VBA COUNTIF function, we are trying to arrive at the result. So to access the function, we need first to use the Worksheet function class.
Code:
Sub Countif_Example1() Range("C3").Value = WorksheetFunction. End Sub
Step 4: From the list, select the Excel VBA COUNTIF function.
Code:
Sub Countif_Example1() Range("C3").Value = WorksheetFunction.CountIf( End Sub
Step 5: If you look at the parameters of the VBA COUNTIF function, we do not see the parameter as we see in the worksheet.
As we can see in the above image in the worksheet, we have exact syntax, but in VBA, we can see only Arg 1 and Arg 2.
Arg 1 is Range, so select the range as A1 to A10.
Code:
Sub Countif_Example1() Range("C3").Value = WorksheetFunction.CountIf(Range("A1:A10"), End Sub
Step 6: Arg 2 is what is the value we need to count from the range A1 to A10. In this example, we need to calculate "Bangalore."
Code:
Sub Countif_Example1() Range("C3").Value = WorksheetFunction.CountIf(Range("A1:A10"), "Bangalore") End Sub
We have completed it now.
Run the code to see the result in cell C3.
We got the result as 4. Since the city name "Bangalore" appears in cells A1, A4, A7, and A10 VBA COUNTIF function returns the product as 4.
If you can see VBA code has returned only the result of the formula, we do not get to know the procedure in the formula bar.
We need to write the code slightly differently to arrive at the formula. Below is the code for applying the formula to the cell.
Code:
Sub Countif_Example1() Range("C3").Formula = "=CountIf(A1:A10, ""Bangalore"")" End Sub
It will apply the formula to cell C3.
Arrive Result with Variables
Variables are an integral part of any coding language. Therefore, we must declare variables to work efficiently with the VBA code. For example, look at the below code.
Code:
Sub Countif_Example2() Dim ValuesRange As Range Dim ResultCell As Range Dim CriteriaValue As String Set ValuesRange = Range("A1:A10") Set ResultCell = Range("C3") CriteriaValue = "Bangalore" ResultCell = WorksheetFunction.CountIf(ValuesRange, CriteriaValue) End Sub
Let us decode the code for you to understand better.
Firstly, we have declared the two variables as Range.
Dim ValuesRange As Range: This is to reference the list of values.
Dim ResultCell As Range: This to reference the result cell.
Then, we have set the range of references to both variables.
Set ValuesRange = Range("A1: A10"): This is the range where all the city names are there.
Set ResultCell = Range("C3"): In this cell, we will store the result of the COUNTIF function.
In the meantime, we have declared one more variable to store the criteria value.
Dim CriteriaValue As String
CriteriaValue = "Bangalore"
So, the variable "CriteriaValue" holds the value "Bangalore."
As usual, we have applied the COUNTIF function in the next line.
ResultCell = WorksheetFunction.CountIf(ValuesRange, CriteriaValue)
Like this, we can apply the COUNTIF function in Excel VBA to fit our needs.