Excel VBA Range.End: Find Last Row and Column
In this article
Excel VBA's Range.End property is the programmatic equivalent of pressing Ctrl+Arrow on the keyboard: it jumps from a cell to the last non-empty cell in a direction. Use Range("A1").End(xlDown) to go down, xlUp, xlToLeft and xlToRight for the other directions. The most common use is finding the last used row in a column: Cells(Rows.Count, "A").End(xlUp).Row. This guide shows exactly how to write it, what it returns, and the traps that make it return the wrong row.
What You'll Need
- Excel with macros enabled (the Developer tab visible, or the ability to open the VBA editor with Alt+F11).
- A workbook saved as .xlsm or .xlsb if you want to keep the code — .xlsx strips macros on save.
- Basic familiarity with the VBA editor: inserting a module and running a macro with F5.
- A sheet with some data, ideally with blank cells in the column you are testing so you can see the edge cases.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or use the Developer tab). In the Project pane, right-click your workbook and choose Insert > Module. A blank code window appears.
- Write the simplest downward jump. Type:
Sub TestEnd()MsgBox Range("A1").End(xlDown).AddressEnd Sub
Put a few values in A1:A5 and run it with F5. The message box shows $A$5 — the last cell before the first blank. Note thatxlDownis a built-in constant equal to -4121; you can write the number, but the constant is clearer. - Understand what End actually does. It works like Ctrl+Arrow: it moves in the given direction until it hits a non-empty cell, then stops at the last non-empty cell before a blank. If the starting cell is empty, it may jump to the next non-empty cell far away, or to the sheet edge. That behaviour is why End is reliable only when you start from a cell you know is filled.
- Find the last row the safe way. Start at the bottom of the column and go up:
Dim lastRow As LonglastRow = Cells(Rows.Count, "A").End(xlUp).RowMsgBox lastRowRows.Countreturns the total number of rows on the sheet (1,048,576 in modern Excel), so you begin at the very bottom and jump up to the last filled cell. This works even when there are blanks in the middle of the column, because you are approaching from below. - Find the last column the same way. Use:
Dim lastCol As LonglastCol = Cells(1, Columns.Count).End(xlToLeft).Column
This starts at the right edge of row 1 and jumps left to the last used column. Change the row number if your headers are on a different row. - Use the result to build a range. Once you have lastRow, you can select or process everything from row 2 down:
Range("A2:A" & lastRow).Select
or loop through it:For i = 2 To lastRowCells(i, 1).Value = UCase(Cells(i, 1).Value)Next i - Handle the empty-column case. If column A is completely empty,
Cells(Rows.Count, "A").End(xlUp).Rowreturns 1 (it lands on the starting cell at the bottom if that cell is empty, then the Row property gives the bottom row — actually it returns the row of the last non-empty cell above, or 1 if none). Test it: clear column A and run the macro. Add a guard:If lastRow < 2 ThenMsgBox "No data"Exit SubEnd If - Qualify the sheet. Unqualified
RangeandCellsrefer to the active sheet, which causes bugs when the user switches sheets. Write:With ThisWorkbook.Worksheets("Data")lastRow = .Cells(.Rows.Count, "A").End(xlUp).RowEnd With
Note the leading dots — they tie the references to the With block. - Test all four directions. Create a small block of data, put the cursor in a corner, and run each of
xlDown,xlUp,xlToLeft,xlToRight. Compare the Address output with what Ctrl+Arrow does manually. This builds intuition for the blank-cell behaviour. - Save and reuse. Save the workbook as .xlsm. Keep a helper function in your module so every macro uses the same logic:
Function LastRow(sh As Worksheet, col As String) As LongLastRow = sh.Cells(sh.Rows.Count, col).End(xlUp).RowEnd Function
Common Mistakes
- Using xlDown from row 1 to find the last row. If row 2 is blank,
Range("A1").End(xlDown)stops at A1 or jumps to the next block, not the true end. Always go up from the bottom. - Forgetting that End stops at blanks. A single empty cell splits the column; End treats the data below the gap as a separate block. Starting from the bottom avoids this entirely.
- Leaving references unqualified.
Cells(Rows.Count, "A")without a sheet prefix follows the active sheet. If the user clicks another tab, the macro reads the wrong data. - Assuming End returns the last row of the used range. It returns the last non-empty cell in that specific column. If column A ends at row 50 but column B has data to row 200, End on A gives 50.
- Comparing End with UsedRange.
UsedRangecan include formatting-only cells and stale ranges; End on a known column is more predictable for data processing.
Tips
- Use
xlUpfromRows.Countas your default last-row pattern; it is the most robust single-line method. - Store
Rows.CountandColumns.Countin variables if you call them repeatedly in a long loop — it is a minor speed gain but good habit. - Combine End with
Offsetto write to the row after the data:Cells(lastRow, 1).Offset(1, 0).Value = "New". - If your data is a proper Excel Table (ListObject), use
ListObjects("Table1").DataBodyRange.Rows.Countinstead; tables resize automatically and End is unnecessary. - Debug with
Debug.Print lastRowand check the Immediate window (Ctrl+G) rather than message boxes during development.
Troubleshooting
- Macro returns 1 when data exists. The column letter is wrong, the data is on another sheet, or the values are formulas returning "". End treats formula blanks as empty; check with Ctrl+Down manually.
- Macro returns 1048576. You used
xlDownfrom an empty cell and it ran to the sheet edge. Switch to the xlUp pattern. - Run-time error 1004. Usually an invalid range reference — for example, lastRow came back as 0 or a column letter was misspelled. Print the value before using it.
- Wrong sheet processed. Qualify every Range and Cells call with a worksheet object, as shown in step 8.
- Results change between runs. The active sheet or selection is changing. Avoid
SelectandActiveSheetin the code; reference sheets by name.
Frequently Asked Questions
What does Range.End actually return?
It returns a Range object pointing at the cell where the Ctrl+Arrow-style jump stops — the last non-empty cell in that direction before a blank, or the sheet edge if the starting cell and path are empty.
What is the difference between xlDown and xlUp?
They are directions, not different methods. xlDown moves toward higher row numbers, xlUp toward lower ones. For finding the last row, xlUp from the bottom is safer because blanks above the data do not fool it.
Can I use End on more than one column at once?
No. End applies to a single cell reference. To find the last row across several columns, run the xlUp pattern on each column and take the maximum, or use UsedRange if formatting artefacts are not a concern.
Why does End skip over blank cells?
It mimics Ctrl+Arrow, which jumps to the next block of data when it encounters a gap. That is by design, and it is why you start from a cell you know is filled — typically the bottom of the column.
Does End work on protected sheets?
Reading cell positions works on protected sheets, but any code that writes to the sheet will fail unless the protection allows it. Unprotect programmatically only if you own the workbook and know the password.
Is End faster than looping through every row?
Yes, considerably. End is a single built-in jump, while a loop checks each cell. For large sheets, the difference is noticeable, so use End to set the loop bounds rather than testing every row for emptiness.