I am currently trying to figure out how to select a range from the second row to the last row, but more specifically between a range of columns. For instance I want to select Range(A2:L2
) to the last row of data in the spreadsheet.
I have tried,
Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:L2" & Lastrow).Select
But this selects from A2:L2
all the way down to the bottom of the spreadsheet. I have checked to see if Lastrow
was incorrect but I printed it to a cell and the correct count of rows displayed.
To find the last row, column, or cell you can use the range's “End” property. The end property allows you to navigate to the end of the data range (to the last cell that is not empty). With this, there are constants that you can use to decide in which direction you want to navigate (top, bottom, left, or right).
To select a range, select a cell, then with the left mouse button pressed, drag over the other cells. Or use the Shift + arrow keys to select the range. To select non-adjacent cells and cell ranges, hold Ctrl and select the cells.
Note: To select the very last cell in a row or column, press END, and then press the RIGHT ARROW key or the DOWN ARROW key.
Try this:
Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:L" & Lastrow).Select
Let's pretend that the value of Lastrow
is 50. When you use the following:
Range("A2:L2" & Lastrow).Select
Then it is selecting a range from A2 to L250.
Sub SelectAllCellsInSheet(SheetName As String)
lastCol = Sheets(SheetName).Range("a1").End(xlToRight).Column
Lastrow = Sheets(SheetName).Cells(1, 1).End(xlDown).Row
Sheets(SheetName).Range("A2", Sheets(SheetName).Cells(Lastrow, lastCol)).Select
End Sub
To use with ActiveSheet:
Call SelectAllCellsInSheet(ActiveSheet.Name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With