EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home VBA VBA Resources VBA Statistical Functions VBA COUNTA

VBA COUNTA

By Madhuri ThakurMadhuri Thakur

VBA COUNTA

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:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

The syntax for VBA COUNTA function is as shown below:

Syntax of COUNTA

Where,

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.

You can download this VBA COUNTA Excel Template here – VBA COUNTA Excel Template

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.

VBA COUNTA Example 1-1

Step 2: Define a new sub-procedure within the newly inserted module, that can hold your macro.

Code:

Sub Example_1()

End Sub

VBA COUNTA Example 1-2

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:

Code:

Sub Example_1()

Sheets("Example 1").Range("A2")

End Sub

VBA COUNTA Example 1-3

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.

Code:

Sub Example_1()

Sheets("Example 1").Range("A2") = WorksheetFunction

End Sub

VBA COUNTA Example 1-4

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.

VBA COUNTA Example 1-5

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.

Code:

Sub Example_1()

Sheets("Example 1").Range("A2") = WorksheetFunction.CountA("Rajnish", "#N/A", 1, "*", True)

End Sub

VBA COUNTA Example 1-6

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.

VBA COUNTA Example 1-7

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.

VBA COUNTA Example 2-1

Follow the below steps:

Step 1: Define a new sub-procedure which can hold your macro.

Code:

Sub Example_2()

End Sub

VBA COUNTA Example 2-2

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.

Code:

Sub Example_2()

Dim rng_1 As Range
Dim op_cell As Range

End Sub

Define two new variables

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.

Code:

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

Set the range

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.

Code:

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

Output initialization

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.

Code:

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

Use WorksheetFunction

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.

VBA COUNTA Example 2-7

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.

Code:

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

Use rng_1 variable

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.

VBA COUNTA Example 2-9

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.

Recommended Articles

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 –

  1. VBA Collection
  2. FileCopy in VBA
  3. Count Cells with Text in Excel
  4. VBA Get Cell Value
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more