VBA & Macro Problems

Why VBA COUNTA Returns Unexpected Results for Arrays in Excel

Rana GarciaRana Garcia Oct 26, 2026 868 views

Question details

The user is experiencing unexpected counting results when applying the worksheet COUNTA function directly to VBA arrays.

Why VBA COUNTA Returns Unexpected Results for Arrays in Excel
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.
Before you start

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.

Solution 1Recommended

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.

1
Define a counter variable

Declare a Long or Integer variable in your VBA script to keep track of the count (e.g., Dim count As Long).

2
Loop through the array

Use a For...Next loop from LBound(yourArray) to UBound(yourArray) to iterate over each element in your array.

3
Check for empty values

Inside the loop, use an If statement (such as If yourArray(i) <> "" Then) to check if the current element contains data.

4
Increment the counter

Add 1 to your counter variable for every non-empty element found during the loop iteration.

Use Custom VBA Logic to Count Non-Empty Array Elements
Reliable Cross-Version Solution: Using a native VBA loop completely bypasses the WorksheetFunction version discrepancies, ensuring your macro runs perfectly on older and newer spreadsheet software.
WPS Spreadsheet Macro Support

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. 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled workbook (.xlsm) to begin editing.
  2. 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. 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. 4. Run your macro: Press F5 or click the Run button to execute the script and retrieve your accurate cell counts instantly.
Seamlessly edit and run standard spreadsheet macrosHigh compatibility with Microsoft Excel .xlsm and .xlsx formatsReliable worksheet functions including COUNTALightweight interface that is easy to navigate and completely free to use
microsoft office alternative - wps office

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.