The fastest way to find the last row in a column with data in Excel VBA is Cells(Rows.Count, "A").End(xlUp).Row for column A, which returns the row number of the last non-empty cell. This method mimics pressing Ctrl+Up from the bottom of the sheet and works in Excel for Microsoft 365, Excel 2021, and earlier versions.
Why Finding the Last Row Matters
When writing VBA macros that loop through data, copy ranges, or apply formatting, you almost always need to know where your data ends. Hard-coding a row number (like Range("A1:A100")) breaks as soon as your data grows or shrinks. Dynamic last-row detection makes your code robust and reusable. Without it, you risk either missing data or processing empty cells, which can cause errors or slow down your macro.
How the End(xlUp) Method Works
The End method in VBA is equivalent to pressing Ctrl+Arrow keys in Excel. xlUp moves upward from a starting cell until it hits a non-empty cell or the first row. The common pattern uses the very last cell in the column (Rows.Count returns 1,048,576 in modern Excel) as the starting point, then jumps up to the last used cell.
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
' lastRow now holds the row number of the last non-empty cell in column A
This works for any column by changing the column letter or index. For column C, use Cells(Rows.Count, "C").End(xlUp).Row. If the column is completely empty, it returns 1 (the header row) or 1 even if row 1 is empty—so always test for that edge case.
Alternative Methods and When to Use Them
While End(xlUp) is the most common, other approaches exist. Each has trade-offs:
- UsedRange:
ActiveSheet.UsedRange.Rows.Countgives the number of rows in the used range, but it may include formatting-only cells and can be unreliable if the sheet has been cleared improperly. - Find method:
Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Rowfinds the last cell with any value, including text, numbers, or errors. It’s more robust for columns with gaps, but slightly slower. - SpecialCells:
Columns("A").SpecialCells(xlCellTypeLastCell).Rowreturns the last used cell on the sheet, not just the column, so it’s not ideal for a single column.
For most cases, End(xlUp) is fast and sufficient. Use Find if your data might have blank rows in the middle and you specifically want the last non-empty cell in that column, ignoring blanks below it.
Common Pitfalls and How to Avoid Them
One frequent mistake is assuming the last row is the same for all columns. If your data has different lengths in each column, using column A might not reflect the true extent of your dataset. For example, if column A has 50 rows but column B has 80, End(xlUp) on column A returns 50, which would miss data in B. In such cases, consider finding the maximum last row across all columns you care about.
Another pitfall: if the column has formulas that return empty strings (""), End(xlUp) treats them as non-empty because they contain a formula. To ignore formula blanks, you might need to loop or use Find with LookAt:=xlWhole and check for values. Also, if the sheet is protected, End may fail; unprotect first or use On Error Resume Next.
Finally, always declare the variable as Long instead of Integer to avoid overflow on large sheets.
Step-by-Step: Writing a Macro That Uses the Last Row
Here’s a practical example: a macro that copies data from column A to column B, starting at row 2, until the last row with data.
- Open the VBA editor with Alt+F11 (Windows) or Option+F11 (Mac).
- Insert a new module via Insert > Module.
- Paste the following code:
Sub CopyColumnData()
Dim lastRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name
' Find last row in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Copy from A2 to lastRow into B2 onwards
ws.Range("A2:A" & lastRow).Copy Destination:=ws.Range("B2")
MsgBox "Copied " & lastRow - 1 & " rows."
End Sub
Run the macro with F5. Remember to save your workbook as a macro-enabled file (.xlsm) and always keep a backup copy because macro actions cannot be undone with Ctrl+Z.
Comparing End(xlUp) with Find for Gaps
Consider a column with data in rows 1, 5, and 10, with blanks in between. End(xlUp) from row 1,048,576 jumps to row 10 (the last non-empty), which is correct. But if you want the last row of a contiguous block starting from a specific row, End(xlDown) from the top would stop at the first blank. The Find method is more flexible for patterns like “last row that has any value in column A, even if there are blanks above it.” Use Find when your data might have trailing blank rows or when you need to search for a specific value.
Frequently Asked Questions
How do I find the last row in a column that has data but with blank cells inside?
If you mean the last non-empty cell regardless of blanks above, End(xlUp) works because it starts from the bottom and jumps to the last non-empty cell. If you mean the last row of a contiguous block starting from row 1, use Cells(1, "A").End(xlDown).Row, but that stops at the first blank.
What if my column contains formulas that return empty strings?
End(xlUp) will treat those as non-empty because the cell contains a formula. To skip them, you can loop from the bottom upward and check Len(Cell.Value) > 0, or use Find with LookAt:=xlWhole and then check the value.
Does this work in Excel for Mac?
Yes, the VBA code is identical on Mac. The only difference is the keyboard shortcut to open the VBA editor (Option+F11) and that some file paths differ, but the End(xlUp) method works the same.
Can I use this to find the last row in a specific column on a different worksheet?
Yes, just reference the worksheet explicitly, like Worksheets("Data").Cells(Rows.Count, "B").End(xlUp).Row. Always qualify the worksheet to avoid accidentally using the active sheet.
Why does my code return row 1 even when the column is empty?
Because End(xlUp) from the bottom goes to row 1 if there are no non-empty cells. You can check if Cells(1, "A").Value = "" and then treat it as no data.
Is there a faster way for very large datasets?
For millions of rows, End(xlUp) is already fast because it uses Excel’s internal navigation. If you need to find the last row across multiple columns, using Find on the entire used range might be more reliable but slightly slower. For extreme performance, consider reading the column into an array and looping backwards, but that’s rarely necessary.
Related
- Random Number SQL Server: RAND, NEWID, and CRYPT_GEN_RANDOM
- Windows + K Shortcut: What It Does and How to Use It
- Shortcut to Switch Off Computer: Fastest Ways
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Open File Dialog: Code and OptionsIn Excel VBA for Windows, use Application.FileDialog(msoFileDialogFilePicker) to display a file picker, let the user choose a file,…
- VBA Loop Through Dictionary Items: Keys, Values, and TipsTo loop through a VBA dictionary’s entries, use For Each on its keys, then retrieve each associated value…
- Excel VBA Dictionary Add: Syntax, Examples, and TipsIn Excel VBA, add an entry to a Dictionary with dict.Add key, item, after creating the Dictionary object.…
- Conditional Format VBA: How to Automate Excel FormattingConditional Format VBA refers to using Excel’s VBA (Visual Basic for Applications) to create, modify, or delete conditional…
- Excel Data Macros: How They Work and When to Use ThemYes—an Excel macro can automate repetitive work on spreadsheet data, such as applying consistent formatting, checking entries, or…
- How Many Employees Does VBA Have? AnswerThe VBA (Victorian Building Authority) employs approximately 400 staff. This figure is based on the authority’s annual reports…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.