Table of Contents
The COUNTA function in VBA is a useful tool for counting the number of non-empty cells in a given range. This function can be applied to any type of data, including strings, numbers, and dates. To use the COUNTA function, the range of cells to be counted must be specified within the parentheses. This function can be used in various scenarios, such as counting the number of responses in a survey, calculating the total number of products in a list, or determining the number of filled cells in a column. Additionally, the COUNTA function can be combined with other functions in VBA to perform more complex calculations, making it a versatile tool for data analysis and manipulation.
Use COUNTA Function in VBA (With Examples)
You can use the CountA method in VBA to count the number of non-empty cells in a specific range.
Here is one common way to use this method in practice:
Sub CountARange()
Range("C1") = WorksheetFunction.CountA(Range("A1:A10"))
End SubThis particular example counts the number of non-empty cells in the range A1:A10 and outputs the result in cell C1.
If you would instead like to display the number of non-empty cells in a message box, you can use the following syntax:
Sub CountARange()
'Create variable to hold results of CountADim counta As Single
'Calculate number of non-empty cells in range
counta = WorksheetFunction.CountA(Range("A1:A10"))
'Display the result
MsgBox "Number of Non-Empty Cells in Range:" & counta
End SubThe following examples shows how to use each of these methods in practice with the following column of values in Excel:

Example 1: Count Non-Empty Cells in Range and Display Results in Cell
Suppose we would like to calculate the number of non-empty cells in the range A1:A10 and output the results in a specific cell.
We can create the following macro to do so:
Sub CountARange()
Range("C1") = WorksheetFunction.CountA(Range("A1:A10"))
End SubWhen we run this macro, we receive the following output:

Notice that cell C1 contains a value of 7.
This tells us that there are 7 non-empty cells in the range A1:A10.
Example 2: Count Non-Empty Cells in Range and Display Results in Message Box
We can create the following macro to do so:
Sub CountARange()
'Create variable to hold results of CountADim counta As Single
'Calculate number of non-empty cells in range
counta = WorksheetFunction.CountA(Range("A1:A10"))
'Display the result
MsgBox "Number of Non-Empty Cells in Range:" & counta
End SubWhen we run this macro, we receive the following output:

The message box tells us that there are 7 non-empty cells in the range A1:A10.
Note that if you’d like to instead count the number of non-empty cells in an entire column you could type A:A instead.
This will calculate the number of non-empty cells in all of column A.
Note: You can find the complete documentation for the VBA CountA method .
Cite this article
stats writer (2024). How do you use the COUNTA function in VBA, and what are some examples of its usage?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-use-the-counta-function-in-vba-and-what-are-some-examples-of-its-usage/
stats writer. "How do you use the COUNTA function in VBA, and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-you-use-the-counta-function-in-vba-and-what-are-some-examples-of-its-usage/.
stats writer. "How do you use the COUNTA function in VBA, and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-use-the-counta-function-in-vba-and-what-are-some-examples-of-its-usage/.
stats writer (2024) 'How do you use the COUNTA function in VBA, and what are some examples of its usage?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-use-the-counta-function-in-vba-and-what-are-some-examples-of-its-usage/.
[1] stats writer, "How do you use the COUNTA function in VBA, and what are some examples of its usage?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How do you use the COUNTA function in VBA, and what are some examples of its usage?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
