Excel VBA COUNTA Function
You may have often used the COUNT and COUNTA functions within Microsoft Excel. Wherein COUNT captures all the numerical values in a cell, COUNTA captures all the non-empty cells (including strings). Is by any means possible to use COUNTA function through VBA? Absolutely, YES! You can use the COUNTA function through VBA as a worksheet function (as it is a part of WorksheetFunction family inside VBA) to count the non-empty cells through a given worksheet range. The benefit of COUNTA over COUNT function is – it can count anything (numbers, strings, special characters, Error-values, etc.) except empty cells in a given range whereas, COUNT can only count the number of cells that consist the numeric values.
VBA COUNTA Syntax:
The syntax for VBA COUNTA function is as shown below:
Arg1 – Specifies the argument for this function which is mandatory and can take any non-empty value such as number, string, error values (like #N/A, #DIV/0!), special characters, etc. rest other arguments are optional.
This function can take a maximum of 30 arguments in a single call. An argument can consist of a range of cells or a single value which is inputted manually.
How to Use COUNTA Function in Excel VBA?
Below are the different examples to use COUNTA Function in Excel using VBA Code.
Example #1 – VBA COUNTA with Manual Arguments as an Input
We will see how COUNTA function works when we provide manual arguments to it in VBA. Follow the below-mentioned steps:
Step 1: Insert a new module in Visual Basic Editor (VBE). Click on Insert tab > select Module.
Step 2: Define a new sub-procedure within the newly inserted module, that can hold your macro.
Sub Example_1() End Sub
Step 3: Decide where we want to store the output for the COUNTA function. I want to store it in cell A2 of sheet named “Example 1”. In order to do that, we need to use Sheets.Range method in VBA. See the following screenshot for your reference:
Sub Example_1() Sheets("Example 1").Range("A2") End Sub
This piece of code selects the cell A2 as a range from the sheet named Example 1.
Step 4: Use an assignment operator so that we can assign the value to the cell that is going to be accessed through the code above. Add the object named WorksheetFunction so that we can access COUNTA function under it.
Sub Example_1() Sheets("Example 1").Range("A2") = WorksheetFunction End Sub
Step 5: Put a dot (.) after object specified and you will see a list of functions available to use under it. Select COUNTA from the list of multiple available functions which allows you to count the non-empty cells or values.
Step 6: Specify the arguments manually under the COUNTA function. Arguments are: “Rajnish”, “#N/A”, 1, “*”, True. We have tried to bring all the data types as an argument to this function.
Sub Example_1() Sheets("Example 1").Range("A2") = WorksheetFunction.CountA("Rajnish", "#N/A", 1, "*", True) End Sub
Step 7: Run this piece of code by hitting F5 or Run button and see the output under cell A2 of sheet “Example 1” in the active workbook.
In this code, we want the system to count the input arguments and store the count under cell A2 of sheet Example 1.
Example #2 – VBA COUNTA to Count Non-Empty Cells in a Given Range
Suppose I have data all across column A and I need to count what are the non-empty rows in the entire column. As the entire column consists of more than 10 Lac’s rows, it is an ideal time-consuming for me to navigate towards and count each non-empty cell. See the partial screenshot of the data below.
Follow the below steps:
Step 1: Define a new sub-procedure which can hold your macro.
Sub Example_2() End Sub
Step 2: Define two new variables under this sub-procedure as a Range using Dim. One variable will be helpful to hold the range of input argument column and other variables will be helpful in holding the cell where we want the output as a counted number.
Sub Example_2() Dim rng_1 As Range Dim op_cell As Range End Sub
Here, rng_1 will store the input range for COUNTA function. The output of COUNTA will be stored under op_cell variable.
Step 3: Now, set the range for both of the variables using VBA Set property. This is to be done because we can’t directly assign a value to a variable defined as a range object.
Sub Example_2() Dim rng_1 As Range Dim op_cell As Range Set rng_1 = Range("A:A") Set op_cell = Range("B1") End Sub
Here, rng_1 variable has been set to the range of entire column named A. op_cell is set to cell B1 as it would be the cell that contains the output of the COUNTA.
Step 4: Now, use general assignment operator against op_cell variable, so that we can store the output under the cell which is set to that variable. This can be considered as an output initialization.
Sub Example_2() Dim rng_1 As Range Dim op_cell As Range Set rng_1 = Range("A:A") Set op_cell = Range("B1") op_cell = End Sub
Step 5: Use WorksheetFunction object on the right-hand side of assignment operator (it would be an expression that will get evaluated and value will be stored under cell set to variable op_cell) to initialize the class within which we can access as well as use COUNTA function.
Sub Example_2() Dim rng_1 As Range Dim op_cell As Range Set rng_1 = Range("A:A") Set op_cell = Range("B1") op_cell = WorksheetFunction End Sub
Step 6: As soon as you hit dot (.) after WorksheetFunction object, you can access all the functions available under that class. Navigate towards the COUNTA function and double click it to select.
Step 7: Use rng_1 variable as an input argument under COUNTA. Therefore, this function under worksheet function object class can count the non-empty cells present within the entire column A.
Sub Example_2() Dim rng_1 As Range Dim op_cell As Range Set rng_1 = Range("A:A") Set op_cell = Range("B1") op_cell = WorksheetFunction.CountA(rng_1) End Sub
Step 8: Run this code by hitting the F5 or Run button and you can see the output as shown below in cell B1 of an active sheet from the workbook.
In cell B1 we can see a number as 17. Which means that, we have 17 non-empty cells present in column A of the worksheet. You may not be seeing at all the 17 through this screenshot as it is a partial one. You better could see the worksheet and navigate through column A.
Things to Remember
- You can use VBA COUNTA function when you have to count the number of non-empty cells present in the given range.
- COUNTA considers all the values like numbers, strings, error values, Booleans, empty text (“”). However, it does not consider the cell which is empty.
- The empty cells will not be counted using the COUNTA function and will be ignored.
- You can use arguments manually in the VBA COUNTA function and it still works.
This is a guide to VBA COUNTA Function. Here we discuss how to use COUNTA Function in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –