Excel VBA: Select Range to Last Row with Data
In this article
To select a range from a starting cell down to the last row with data in Excel VBA, use the End(xlUp) method: Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Select. This selects from A1 to the last populated cell in column A. The same pattern works for any column—just change the column letter or index. This guide explains the exact code, how to adapt it, and how to avoid the common pitfalls that make VBA select the wrong range.
What You'll Need
- Microsoft Excel (any modern version) with macros enabled.
- Access to the VBA editor: press
Alt + F11(Windows) orFn + Option + F11(Mac). - A worksheet containing data, ideally with a header row and no completely blank rows in the target column.
- Basic familiarity with inserting a module and running a macro.
Step-by-Step
- Open the VBA editor. In Excel, press
Alt + F11(Windows) orFn + Option + F11(Mac). The Visual Basic Editor opens. - Insert a new module. In the editor, go to Insert > Module. A blank code window appears.
- Write the basic selection macro. Paste the following code into the module:
This finds the last used row in column A and selects from A1 to that row. ChangeSub SelectToLastRow() Dim lastRow As Long lastRow = Cells(Rows.Count, "A").End(xlUp).Row Range("A1:A" & lastRow).Select End Sub"A"to your actual column letter if needed. - Run the macro. Close the editor or press
F5while the cursor is inside the subroutine. The range is selected on the active sheet. - Adapt for a different starting cell. If your data starts at A2 (no header), change
Range("A1:A" & lastRow)toRange("A2:A" & lastRow). To start at a different column, replace both instances of"A"with the new column letter. - Select multiple columns. To select from column A to the last row across columns A:C, use:
EnsureRange("A1:C" & lastRow).SelectlastRowis calculated from a column that reliably indicates the data extent (often column A). - Use the last row for other actions. Instead of
.Select, you can assign the range to a variable or act on it directly, e.g.,Range("A1:A" & lastRow).CopyorRange("A1:A" & lastRow).Value = "". - Test with different data layouts. Try a sheet with blank cells in column A but data in other columns. The macro will stop at the last non-blank cell in column A, which may not be the true last row of the dataset. Adjust the column reference to a column that is always populated (e.g., an ID column).
Common Mistakes
- Using
ActiveSheetimplicitly. The code above acts on the active sheet. If the wrong sheet is active, you'll select the wrong range. Qualify with a worksheet variable:Worksheets("Sheet1").Range(...). - Assuming
End(xlUp)finds the absolute last row. It finds the last non-empty cell in the specified column, starting from the bottom. If the column has gaps, it stops at the first gap from the bottom. - Forgetting to handle an empty column. If column A is completely empty,
End(xlUp)returns row 1 (the first row), so the range becomes A1:A1—likely not what you want. Add a check:If lastRow = 1 And IsEmpty(Cells(1, "A")) Then MsgBox "No data". - Using
Rows.Countwithout a sheet reference.Rows.Countrefers to the active sheet. For clarity and safety, usews.Rows.Countwherewsis your worksheet variable. - Selecting instead of acting directly.
.Selectis slow and unnecessary for most operations. Work with the range object directly to improve performance and avoid screen flicker.
Tips
- Use
UsedRangeas an alternative when you need the last row across the entire used area:lastRow = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Row - 1. This can be less reliable if the sheet has formatting far below the data. - For a dynamic last row that accounts for blanks, use
Cells.FindwithSearchOrder:=xlByRows, SearchDirection:=xlPreviousto locate the true last cell. - Store the last row in a
Longvariable to avoid overflow on very large sheets (though Excel's row limit is well within Long range). - Turn off
ScreenUpdatingwhile your macro runs to speed up execution, and turn it back on at the end. - Use
Option Explicitat the top of the module to catch undeclared variables.
Troubleshooting
If the macro selects only the header or a single cell, the target column may be empty or contain only a header. Verify the column letter and that data exists below the header. If it selects too many rows, check for stray formatting or values far down the column—clear unused cells or use a column that is tightly populated. If you get a "Subscript out of range" error, the worksheet name in your code doesn't match any open sheet; correct the name or use the index. If the selection appears on the wrong sheet, ensure you've qualified the range with the intended worksheet object. For Mac users, keyboard shortcuts differ; use the menu to run macros if F5 doesn't work.
Frequently Asked Questions
How do I select to the last row with data in any column, not just A?
Replace "A" with the column letter you want to use as the reference. For example, Cells(Rows.Count, "C").End(xlUp).Row finds the last row in column C.
Why does my macro select row 1 when there is data?
This happens when the reference column is completely empty. End(xlUp) from the bottom of an empty column returns row 1. Make sure the column you reference contains data, or add a check for empty columns.
Can I select the entire row instead of just a range?
Yes. Use Rows(lastRow).Select to select the entire row, or Range("A" & lastRow & ":Z" & lastRow).Select for a specific portion.
What is the difference between End(xlUp) and UsedRange?
End(xlUp) finds the last non-empty cell in a single column, which is precise for that column. UsedRange covers all cells that have ever been used (including formatting), which can overstate the data range.
How do I avoid selecting and just work with the range?
Assign the range to a variable: Set rng = Range("A1:A" & lastRow), then use rng.Copy, rng.Value, etc. This is faster and cleaner than selecting.
Does this work on protected sheets?
Selecting works on protected sheets, but modifying cells does not. You'll need to unprotect the sheet first if your macro writes data.