Why VBA COUNTA Returns Unexpected Results for Arrays in Excel
Question details
The user is experiencing unexpected counting results when applying the worksheet COUNTA function directly to VBA arrays.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Using the WorksheetFunction.CountA within a VBA script to count non-empty elements in an array.
- Observed behavior
- The COUNTA function handles arrays differently depending on the Excel version (counting elements vs. counting data), leading to inconsistent or unexpected counts.
Verify your current Excel version, as the behavior of the worksheet COUNTA function when applied to arrays changed starting in Excel 2010. Ensure you know whether you need to count worksheet cells or elements within an in-memory VBA array.
Use Custom VBA Logic to Count Non-Empty Array Elements
Since the worksheet COUNTA function is designed for Range objects, it is safer to use a custom VBA loop to count non-empty elements within an in-memory array.
Relying on worksheet functions for internal VBA array operations can lead to version-specific bugs. Writing a dedicated loop ensures consistent behavior across all versions.
Declare a Long or Integer variable in your VBA script to keep track of the count (e.g., Dim count As Long).
Use a For...Next loop from LBound(yourArray) to UBound(yourArray) to iterate over each element in your array.
Inside the loop, use an If statement (such as If yourArray(i) <> "" Then) to check if the current element contains data.
Add 1 to your counter variable for every non-empty element found during the loop iteration.

Pass Range Objects to the COUNTA Function
To avoid array-related inconsistencies, apply the WorksheetFunction.CountA directly to a worksheet Range rather than a VBA array.
Run Macros and Count Elements Smoothly with WPS Spreadsheet
WPS Office Spreadsheet provides robust compatibility with standard VBA scripts and worksheet functions. You can reliably count non-empty cells using COUNTA on Range objects directly within the WPS Macro Editor.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled workbook (.xlsm) to begin editing.
- 2. Access the Macro Editor: Navigate to the Tools tab and click on 'Macros', or press ALT + F11 to open the built-in VBA editor.
- 3. Write the COUNTA script: Use Application.WorksheetFunction.CountA(Range("A1:A10")) in your module to reliably count non-empty cells on the worksheet.
- 4. Run your macro: Press F5 or click the Run button to execute the script and retrieve your accurate cell counts instantly.

Frequently Asked Questions
Why does COUNTA work differently in Excel 2010 and newer versions?
Starting in Excel 2010, WorksheetFunction.CountA was updated to count the number of elements within an array rather than evaluating the actual data inside those elements. This causes it to return the total size of the array rather than the count of non-empty items.
Can I use the UBound function instead of COUNTA in VBA?
Yes, for in-memory VBA arrays, using UBound(array) - LBound(array) + 1 is the standard way to find the total number of elements. However, to find only the non-empty elements, you must loop through the array and count them manually.
Does COUNTA count cells with formulas that return blank?
Yes, the COUNTA function considers a cell containing a formula as non-empty, even if that formula evaluates to an empty string (""). To ignore these, you may need a custom VBA loop or to use COUNTBLANK.




