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 SheetCells.Find— Find a record in a dynaset-type or snapshot-type DAO RecordsetMyRange.Column— Fill a Value Down into Blank Cells in a ColumnMyRange.Column— Hide and Unhide ColumnsRange— Refer to Cells by Using a Range Object
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
— top 7%.