How can I find last row that contains data in the Excel sheet with a macro?

calendar_today Asked Sep 16, 2008
thumb_up 30 upvotes
history Updated April 16, 2026

Question posted 2008 · +41 upvotes

How can I find the last row that contains data in a specific column and on a specific sheet?

Accepted answer +30 upvotes

How about:

 Sub GetLastRow(strSheet, strColum)
 Dim MyRange As Range
 Dim lngLastRow As Long

    Set MyRange = Worksheets(strSheet).Range(strColum & "1")

    lngLastRow = Cells(Rows.Count, MyRange.Column).End(xlUp).Row
 End Sub

Re Comment

This

  Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row

Will return the row number of the last cell even when only a single cell in the last row has data.

2 code variants in this answer

  • Variant 1 — 8 lines, starts with Sub GetLastRow(strSheet, strColum)
  • Variant 2 — 1 lines, starts with Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPre…

Excel VBA objects referenced (5)

  • Cells.Find — Find All the Sparklines on a Sheet
  • Cells.Find — Find a record in a dynaset-type or snapshot-type DAO Recordset
  • MyRange.Column — Fill a Value Down into Blank Cells in a Column
  • MyRange.Column — Hide and Unhide Columns
  • Range — Refer to Cells by Using a Range Object

Top excel-vba Q&A (6)

+30 upvotes ranks this answer #10 out of 136 excel-vba solutions on this site — top 7%.