The fastest way to find the last used row in Excel VBA is ws.Cells(ws.Rows.Count, 1).End(xlUp).Row, which returns the row number of the last non-empty cell in column A for worksheet ws. This method is the standard, reliable choice for most data sets, though alternatives exist for blank rows, filtered data, or entire-sheet scans.
Why the Last Used Row Matters
In VBA, you rarely know how many rows of data exist in advance. Loops, copy-paste operations, and range references all need a defined end point. Hardcoding a row number (like Range("A1:A100")) breaks when data grows or shrinks. The last used row gives you a dynamic boundary so your macro processes exactly the data present, no more and no less.
Common uses include:
- Looping through rows to apply formatting or calculations
- Copying a variable-sized range to another sheet
- Finding where to append new data
- Clearing or deleting only the populated region
The Classic End(xlUp) Method
The most common approach mimics pressing Ctrl+Up from the bottom of a column. In VBA, End(xlUp) is the equivalent. The syntax is:
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Here, ws.Rows.Count returns 1,048,576 on modern Excel (the last row number). Starting from that cell in column 1 (A), End(xlUp) jumps up to the last non-empty cell, and .Row gives its row number. If column A is completely empty, it returns 1 (the starting row), so check for that if your data might be empty.
To use a specific column, change the second argument: ws.Cells(ws.Rows.Count, "B") for column B. To use the active sheet, replace ws with ActiveSheet, but always prefer an explicit worksheet reference to avoid errors when the user switches sheets.
Alternative: Find Method for Gaps
The End(xlUp) method stops at the first non-empty cell from the bottom, but it ignores blank cells in between. If your data has gaps (e.g., row 5 is blank, but row 10 has data), End(xlUp) from the bottom will correctly return 10 because it scans upward until it hits any non-empty cell. However, if you need the last row that contains any value in a specific column, and that column has blanks in the middle, the result is still correct—it finds the very last non-empty cell, not the last row of a continuous block.
For a more robust scan that handles hidden rows or filtered data, you can use the Find method:
Dim lastRow As Long
Dim rng As Range
Set rng = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If Not rng Is Nothing Then
lastRow = rng.Row
Else
lastRow = 0 ' no data
End If
This searches the entire sheet for any cell with content (including formulas) and returns the row of the last match. It’s slower than End(xlUp) but more thorough, especially if your last row isn’t in column A.
Using UsedRange for the Whole Sheet
Another built-in property is ws.UsedRange, which returns the rectangular range that encloses all used cells. To get the last row of that range:
Dim lastRow As Long
lastRow = ws.UsedRange.Row + ws.UsedRange.Rows.Count - 1
This gives the bottom row of the used range. However, UsedRange can be unreliable: it may include cells that were formatted but are now empty, or it may not update after you delete data until you save or recalculate. It also includes any cell that ever had formatting, so it can return a larger row than expected. Use it only when you need the entire sheet’s extent, not just a single column’s data.
Handling Blank Rows and Filters
If your data has blank rows in the middle and you want the last row of a contiguous block starting at row 1, you might need a different approach. For example, if you have headers in row 1 and data in rows 2-5, row 3 is blank, but rows 4-5 have data, End(xlUp) from the bottom of column A returns 5, which is correct. But if you want the last row of the first continuous block (row 2 in this case), you’d need to loop or use End(xlDown) from the top, which stops at the first blank cell.
For filtered data, End(xlUp) still works on the visible cells only if you use SpecialCells(xlCellTypeVisible) first, but it’s simpler to use the Find method with LookIn:=xlValues to ignore hidden rows. In practice, most macros run on unfiltered data, so End(xlUp) is fine.
Common Mistakes and Pitfalls
- Using
Rows.Counton the wrong sheet: Always qualify with the worksheet object, likews.Rows.Count, notRows.Countalone, which refers to the active sheet. - Assuming column A has data: If your data starts in column B,
End(xlUp)on column A will return 1. Always reference the correct column. - Forgetting to declare variables: Use
Dim lastRow As Longto avoid overflow on large sheets. - Not handling empty sheets: If the column is empty,
End(xlUp)returns 1, which might be interpreted as a header row. Add a check:If lastRow = 1 And IsEmpty(ws.Cells(1, 1)) Then lastRow = 0. - Using
UsedRangeafter deletions: It may not shrink until you save or press Ctrl+End. Usews.Cells(ws.Rows.Count, 1).End(xlUp).Rowfor a fresh read.
Performance and Best Practices
For most sheets, End(xlUp) is instantaneous. If you have tens of thousands of rows, it’s still fast. Avoid looping through every cell to find the last row—that’s slow. Always set Application.ScreenUpdating = False and Application.EnableEvents = False before running a macro that modifies data, and restore them in a Finally block. Remember to save a copy of your workbook before running a macro, as macros cannot be undone with Ctrl+Z.
For a full example, here’s a macro that copies data from column A to column B starting at the last row:
Sub CopyLastRowData()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
If lastRow > 0 Then
ws.Range("A1:A" & lastRow).Copy Destination:=ws.Range("B1")
End If
End Sub
This copies the entire column A data to column B. Always test on a copy of your workbook first.
Frequently Asked Questions
How do I find the last used row in a specific column, not column A?
Change the column reference in the Cells property: ws.Cells(ws.Rows.Count, "D").End(xlUp).Row for column D. Replace the column letter with any letter or number.
What if my data has blank rows in the middle?
End(xlUp) still works because it scans from the bottom until it finds any non-empty cell, regardless of gaps. It returns the very last row with data, not the last row of a contiguous block. If you need the last row of a contiguous block from the top, use ws.Cells(1, 1).End(xlDown).Row but that stops at the first blank cell.
Does UsedRange give the same result?
Not always. UsedRange includes any cell that has ever been formatted or edited, even if it’s now empty. It can return a larger row number than the actual last data row. Use End(xlUp) for precise data boundaries.
How do I find the last used row on a different worksheet?
Reference the worksheet explicitly: Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Data") then use ws.Cells(ws.Rows.Count, 1).End(xlUp).Row. Never rely on ActiveSheet unless you’re sure the user is on the right sheet.
What is the difference between End(xlUp) and End(xlDown)?
End(xlUp) starts from the bottom of the sheet and goes up to the last non-empty cell. End(xlDown) starts from the top and goes down to the first blank cell. Use xlUp to find the last row with data; use xlDown to find the end of a contiguous block starting at a known cell.
Can I find the last used row without VBA?
Yes, you can press Ctrl+End to jump to the last used cell, or use the formula =COUNTA(A:A) to count non-empty cells, but that doesn’t give the row number directly. For a dynamic range in formulas, use =INDEX(A:A,COUNTA(A:A)) to return the last value, but for VBA, the methods above are standard.
Related
- Shortcut for cmd? (Open Command Prompt Fast)
- Computer Skills Test for Data Entry: What It Covers
- Generating Random Numbers in Python: A Complete Guide
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Array Contains: How to Check for a Value in ExcelA VBA array contains multiple values stored under one variable name, with each value reached by an index…
- Copy and Paste Values in VBA: Methods and ExamplesTo copy only cell values with Excel VBA, assign one range’s Value2 to another range of the same…
- VBA Coding Dictionary: How the Dictionary Object WorksA VBA Dictionary is an object that stores pairs of keys and values, letting an Excel macro look…
- Convert an Excel Range to HTML in VBATo convert a range to a formatted HTML file in Excel for Microsoft 365 on Windows, add a…
- VBA Find in Range: Search Cells and Return MatchesTo find a value in an Excel range with VBA, use the range’s Find method and check whether…
- Refresh Pivot Table Using VBA: Code and TipsTo refresh a PivotTable with VBA in Excel for Microsoft 365 on Windows, call its RefreshTable method; to…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.