Smart Systems for Home

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

Excel VBA: Select Last Row 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 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

  1. 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.
  2. Write the basic selection statement. In the new module, type a Sub procedure. The core line uses the ListObjects collection. For a table named SalesData on the active sheet, write:
    Sub SelectLastRow()
    Dim tbl As ListObject
    Set tbl = ActiveSheet.ListObjects("SalesData")
    tbl.DataBodyRange.Rows(tbl.DataBodyRange.Rows.Count).Select
    End Sub
  3. 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.
  4. 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.
  5. Handle an empty table. If the table has only a header row, DataBodyRange is Nothing and the code errors. Add a guard:
    If tbl.DataBodyRange Is Nothing Then
    MsgBox "Table is empty"
    Exit Sub
    End If
  6. 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.
  7. 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).
  8. 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. DataBodyRange excludes the header, which is usually what you want. If you use Range or UsedRange instead, 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, DataBodyRange still refers to data rows only, so the last data row is correct, but selecting EntireRow will include the totals row if it sits directly below.
  • Running the macro while a different sheet is active. Unqualified references like ActiveSheet.ListObjects fail 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.Goto instead of Select if 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.Add to 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 DataBodyRange is Nothing. 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.Count counts 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. Use DataBodyRange.Rows(...).Select to 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.