Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: How to Count Rows in a Table

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To count the rows in an Excel Table (ListObject) using VBA, use the ListRows.Count property of the table object. For example, ActiveSheet.ListObjects("Table1").ListRows.Count will return the number of data rows, excluding the header row. This is the most reliable method because it targets the table structure specifically, regardless of where the table is positioned on the worksheet or if there are empty cells within the data range.

What You'll Need

  • Microsoft Excel installed on Windows or macOS.
  • A workbook containing at least one formatted Table (created via Insert > Table or Ctrl+T).
  • The Developer tab enabled in the Excel Ribbon to access the Visual Basic Editor (VBE).
  • The exact name of the table you wish to count (found under the Table Design tab when a cell in the table is selected).

Step-by-Step

  1. Open the Visual Basic Editor: Press Alt + F11 on your keyboard. This opens the VBE window where all your macros and modules are stored.
  2. Insert a New Module: In the top menu, go to Insert > Module. This creates a blank space where you can write your custom VBA procedure.
  3. Declare Your Variables: Start your subroutine by defining the variables you will use. Using specific types like ListObject and Long prevents memory errors and makes the code easier to read. For example:
    Dim myTable As ListObject
    Dim rowCount As Long
  4. Assign the Table to the Variable: You must tell VBA which table you are referring to. You can do this by referencing the worksheet and the table name.
    Set myTable = ActiveSheet.ListObjects("Table1")
    Replace "Table1" with the actual name of your table.
  5. Count the Data Rows: Use the ListRows.Count property. This specifically counts the rows that contain data, ignoring the header row.
    rowCount = myTable.ListRows.Count
  6. Display the Result: To verify the code is working, use a message box to pop up the final number.
    MsgBox "The table has " & rowCount & " data rows."
  7. Run the Macro: Press F5 while the cursor is inside the subroutine, or return to Excel and run the macro via the Developer > Macros menu.

Common Mistakes

One of the most frequent errors is confusing a standard Range with a ListObject. Many users try to use Range("A1").End(xlDown).Row to count rows. While this works for simple lists, it fails if there are blank cells in the column, as it stops at the first gap. Using ListObjects ensures you count the entire table boundary regardless of cell contents.

Another common mistake is forgetting to account for the header row. If you use myTable.Range.Rows.Count, VBA counts every row in the table's total footprint, including the headers. If your table has 10 rows of data and 1 header row, Range.Rows.Count returns 11, whereas ListRows.Count returns 10. Depending on whether you are looping through data or calculating total space, using the wrong property can cause "Off-by-One" errors in your logic.

Users also frequently hard-code the table name. If you rename "Table1" to "SalesData" in the Excel interface but do not update the VBA string, the code will throw a "Subscript out of range" error. To avoid this, ensure the string in your code exactly matches the name shown in the Table Design tab.

Tips

If you want to make your code more dynamic, avoid using ActiveSheet. If the user happens to be on a different sheet when the macro runs, the code will crash because it cannot find the table on the current active page. Instead, reference the sheet by its code name or tab name, such as Worksheets("DataSheet").ListObjects("Table1").

For those building larger applications, consider creating a function that returns the row count. This allows you to call the count from multiple different macros without rewriting the logic. A custom function like Function GetTableRows(tableName As String) As Long makes your project much more maintainable.

If your table is very large (hundreds of thousands of rows), avoid using a For Each` loop to count rows manually. The .Count property is an internal Excel calculation and is nearly instantaneous, whereas looping through every row to increment a counter will significantly slow down your workbook and may cause Excel to freeze.

Troubleshooting

If your code does not run as expected, first check for the "Subscript out of range" error. This almost always means VBA cannot find the table name you provided. Double-check the spelling and ensure there are no leading or trailing spaces in the table name within the Excel Table Design menu.

If the count is returning a number that is one higher than expected, you are likely using myTable.Range.Rows.Count instead of myTable.ListRows.Count. Switch to ListRows to exclude the header.

If you receive a "Object Required" error, it usually means the Set keyword was omitted. In VBA, when assigning an object (like a Table or a Worksheet) to a variable, you must use Set myTable = .... Using myTable = ... without the Set keyword will result in a runtime error because VBA tries to assign a value instead of a reference.

If the code works on your machine but not on a colleague's, ensure the workbook is saved as an .xlsm (Excel Macro-Enabled Workbook) or .xlsb (Excel Binary Workbook). Standard .xlsx files cannot store VBA code, and the macros will be stripped out upon saving.

Frequently Asked Questions

Does ListRows.Count include the total row?

No, ListRows.Count only counts the data rows. If you have the "Total Row" enabled at the bottom of your table, it is not included in this count.

How do I count rows in a table if I don't know the table name?

You can reference the table by its index number. For example, ActiveSheet.ListObjects(1).ListRows.Count will count the rows in the first table created on that specific worksheet.

Can I count rows in a table that is filtered?

The ListRows.Count property returns the total number of rows in the table, regardless of whether they are hidden by a filter. To count only visible rows, you would need to loop through the ListRows and check if the row's Hidden property is false.

What is the difference between ListRows.Count and Range.Rows.Count?

ListRows.Count counts only the data entries. Range.Rows.Count counts the entire table area, which includes the header row and any total rows if they are enabled.

Will this work if the table is empty?

Yes, if the table exists but has no data rows (only the header), ListRows.Count will return 0. However, if the table object itself has been deleted, the code will error out.