VBA COUNTIF
Criteria-based functions are the rulers of Excel in calculations. At the beginning of learning Excel, we must have learned the COUNTIF process in Excel. In our earlier articles, we have shown you how to work with the COUNTIF function in Excel VBA.
Refer to our article on the COUNTIF Formula in ExcelCOUNTIF Formula In ExcelThe COUNTIF function in Excel counts the number of cells within a range based on pre-defined criteria. It is used to count cells that include dates, numbers, or text. For example, COUNTIF(A1:A10,”Trump”) will count the number of cells within the range A1:A10 that contain the text “Trump” read more to learn the basics of the COUNTIF function in Excel VBA. This article will show you how to use the same function in VBA coding. Now, we will see the same formula in VBA. First, COUNTIF is not a VBA function but a worksheet function that we can access under the worksheet function class.
You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA COUNTIF (wallstreetmojo.com)
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 ExplicitOption ExplicitVBA option explicitly makes a user mandatory to declare all the variables before using them; any undefined variable will throw an error while coding execution. We can enable it for all codes from options to require variable declaration.read more
Sub Countif_Example1()
End Sub
Step 2: Since we need to store the result in cell C3, start the Range(“C3”).Value.
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.
Sub Countif_Example1()
Range(“C3”).Value = WorksheetFunction.
End Sub
Step 4: From the list, select the Excel VBA COUNTIF function.
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.
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.”
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.
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 codeVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more. For example, look at the below 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.
Recommended Articles
This article has been a guide to VBA COUNTIF. Here, we look at the working of the COUNTIF Function in Excel VBA along with practical examples and a downloadable Excel template. Below are some useful articles related to VBA: –
- VBA Like OperatorCount in VBACountif not Blank in ExcelCOUNTIF Examples in Excel