Fix VBA UsedRange Returning Different Row Counts in Excel
Question details
Users need a reliable way to count rows in a VBA report because the UsedRange property returns inconsistent row counts for different users.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Running a VBA macro to count the rows of a refreshed query table to process data or generate reports.
- Observed behavior
- The UsedRange.Rows.Count method returns different numbers for different users even when the query data is identical, likely capturing hidden formatting or previously used blank cells.
Before modifying your VBA script, ensure you have saved a backup of your current workbook and that your query tables are refreshing successfully.
Use ResultRange Instead of UsedRange for Query Tables
Replace the unreliable UsedRange method with the ResultRange property to accurately target only the refreshed output of your query table.
The UsedRange property in Excel VBA can be unreliable because it tracks any cell that has ever contained data or formatting. If users apply formatting to blank rows or delete data without removing the rows entirely, UsedRange expands to include them. By utilizing the ResultRange property of the QueryTable object, you can pinpoint the exact data returned by the query, ignoring stray formatting.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor and locate the module containing your row-counting script.
To find the exact names of your query tables, you can run a loop in the Immediate Window: For Each ws In ActiveWorkbook.Worksheets: For Each qt In ws.QueryTables: Debug.Print qt.Name: Next qt: Next ws.
Replace your existing UsedRange.Rows.Count line with ResultRange.Rows.Count targeting your specific query table (e.g., ActiveSheet.QueryTables("YourTableName").ResultRange.Rows.Count).
If you need to find the absolute last row number rather than just the total count, calculate it using: ResultRange.Row + ResultRange.Rows.Count - 1.

Write and Run VBA Macros Seamlessly with WPS Spreadsheet
WPS Office provides robust support for VBA and macros, allowing you to automate tasks and manage query tables with high precision. Experience fast performance and accurate data handling with WPS Spreadsheet without the hefty subscription fees.
- 1. Install WPS Office: Download and install the free WPS Office suite on your computer.
- 2. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the query table.
- 3. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click 'VBA Editor' to access your scripts.
- 4. Execute the Macro: Update your VBA code to use the ResultRange method and run the macro to get accurate row counts.

Frequently Asked Questions
Why does the VBA UsedRange include empty cells?
The UsedRange property tracks the furthest cell in a worksheet that has ever contained data or formatting. If a user manually formats a blank cell, applies a border, or deletes contents without deleting the actual row, VBA still considers that space as 'used'.
How do I clear the UsedRange memory in a spreadsheet?
You can force the application to reset the UsedRange by highlighting all unused rows and columns below and to the right of your actual data, deleting them entirely (not just clearing contents), and then saving the workbook. Alternatively, running ActiveSheet.UsedRange in VBA can sometimes trigger a recalculation.
What is the difference between UsedRange and ResultRange?
UsedRange applies to the entire worksheet and encompasses any cell with data, formulas, or formatting. ResultRange is a specific property of a QueryTable object that only encompasses the exact range of cells populated by the most recent external data query.
Can I use ListObjects to count rows instead of QueryTables?
Yes. If your external data is loaded into a modern Excel Table (ListObject) rather than an older QueryTable, you can count the rows using ActiveSheet.ListObjects("TableName").DataBodyRange.Rows.Count, which provides the same accuracy as ResultRange.




