Excel VBA: Select Last Row in a Table
In this article
To select the last row of an Excel table using VBA, use the table's DataBodyRange and its Rows.Count property: ActiveSheet.ListObjects("Table1").DataBodyRange.Rows(ActiveSheet.ListObjects("Table1").DataBodyRange.Rows.Count).Select. This selects the entire last data row of the table, ignoring the header and any totals row. The steps below show how to adapt this for any table name, avoid common errors, and handle empty tables safely.
What You'll Need
- Excel with macros enabled (the Developer tab visible, or the ability to open the VBA editor with Alt+F11).
- A worksheet containing a formatted Excel table (created with Insert > Table, not just a range with borders).
- The table's name, which you can find on the Table Design tab in the Properties group.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or use the Developer tab > Visual Basic). In the Project Explorer, right-click your workbook and choose Insert > Module.
- Write the basic selection statement. In the new module, type a Sub procedure. The core line uses the ListObjects collection. For a table named
SalesDataon the active sheet, write:Sub SelectLastRow()Dim tbl As ListObjectSet tbl = ActiveSheet.ListObjects("SalesData")tbl.DataBodyRange.Rows(tbl.DataBodyRange.Rows.Count).SelectEnd Sub - Run the macro. Place the cursor inside the Sub and press F5, or go back to Excel and use Developer > Macros, select
SelectLastRow, and click Run. The last data row of the table should become the active selection. - Make it work on any sheet. If the table might not be on the active sheet, qualify the worksheet explicitly, for example
Set tbl = ThisWorkbook.Worksheets("Data").ListObjects("SalesData"). Replace the sheet name with your actual sheet name. - Handle an empty table. If the table has only a header row,
DataBodyRangeisNothingand the code errors. Add a guard:If tbl.DataBodyRange Is Nothing ThenMsgBox "Table is empty"Exit SubEnd If - Select the whole row instead of just the data cells. If you need the entire worksheet row (columns A to the last column), use
tbl.DataBodyRange.Rows(tbl.DataBodyRange.Rows.Count).EntireRow.Select. Be aware this selects cells outside the table as well. - Select only the last cell in a specific column. To target the last cell of the third table column, use
tbl.DataBodyRange.Cells(tbl.DataBodyRange.Rows.Count, 3).Select. Change the 3 to the column's position within the table (not the worksheet column letter). - Test with a copy of your data. Save your workbook first, then run the macro on a duplicate sheet to confirm it selects the intended row before using it in production.
Common Mistakes
- Using
End(xlUp)on the whole column. That finds the last used row of the worksheet, not the table, and can land on unrelated data below the table. - Forgetting the header row.
DataBodyRangeexcludes the header, which is usually what you want. If you useRangeorUsedRangeinstead, you may include the header and get an off-by-one error. - Hard-coding the table name incorrectly. Table names are case-sensitive in the ListObjects collection and cannot contain spaces. Check the exact name on the Table Design tab.
- Ignoring the totals row. If the table has a totals row enabled,
DataBodyRangestill refers to data rows only, so the last data row is correct, but selectingEntireRowwill include the totals row if it sits directly below. - Running the macro while a different sheet is active. Unqualified references like
ActiveSheet.ListObjectsfail if the table is not on the active sheet.
Tips
- Assign the macro to a button (Developer > Insert > Button) for one-click selection.
- Use
Application.Gotoinstead ofSelectif you want to scroll the last row into view without changing the selection:Application.Goto tbl.DataBodyRange.Rows(tbl.DataBodyRange.Rows.Count). - Store the table name in a variable or a named range so you only change it in one place.
- If you plan to write data to the last row, use
tbl.ListRows.Addto append a new row rather than selecting and typing, which keeps the table structure intact.
Troubleshooting
- Run-time error 9 (Subscript out of range): The table name is wrong or the table is on a different sheet. Verify the name and qualify the worksheet.
- Run-time error 91 (Object variable not set): The table is empty, so
DataBodyRangeisNothing. Add the empty-table guard shown above. - Macro selects the wrong row: Check whether the table has a totals row or hidden rows.
DataBodyRange.Rows.Countcounts visible and hidden rows, so hidden rows still count. - Nothing happens when you run it: Confirm macros are enabled and that you are running the correct Sub. Check the Immediate Window (Ctrl+G) for errors.
- The selection includes cells outside the table: You likely used
EntireRow. UseDataBodyRange.Rows(...).Selectto stay within the table's columns.
Frequently Asked Questions
Can I select the last row without knowing the table name?
Yes, if there is only one table on the sheet: ActiveSheet.ListObjects(1).DataBodyRange.Rows(ActiveSheet.ListObjects(1).DataBodyRange.Rows.Count).Select. For multiple tables, loop through ListObjects or use the name.
Does this work on Excel for Mac?
Yes, the VBA object model for ListObjects is the same. Use the Visual Basic editor from the Developer tab; keyboard shortcuts may differ slightly.
How do I select the last row of a filtered table?
DataBodyRange includes hidden (filtered-out) rows. To act on visible rows only, use tbl.DataBodyRange.SpecialCells(xlCellTypeVisible) and then the last area, but note that this can error if no rows are visible.
What if the table has a totals row?
DataBodyRange excludes the totals row, so the last data row is still correct. To select the totals row itself, use tbl.TotalsRowRange.Select.
Can I use this in a loop to process each row?
Yes, but selecting inside a loop is slow. Work with the range directly, for example For Each r In tbl.DataBodyRange.Rows, and avoid Select unless you need the visual highlight.