How to choose the range of the second row for the last row

I'm currently trying to figure out how to select a range from the second row to the last row, but more specifically between the range of columns. For example, I want to select Range ( A2:L2 ) for the last row of data in a spreadsheet.

I tried,

 Dim Lastrow As Integer Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row Range("A2:L2" & Lastrow).Select 

But this is selected from A2:L2 all the way to the bottom of the spreadsheet. I checked if Lastrow was wrong, but I typed it in a cell and the correct number of rows was displayed.

+4
source share
2 answers

Try the following:

 Dim Lastrow As Integer Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row Range("A2:L" & Lastrow).Select 

Suppose the value of Lastrow is 50. When you use the following:

 Range("A2:L2" & Lastrow).Select 

He then selects a range from A2 to L250.

+14
source
 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 

Use with ActiveSheet:

 Call SelectAllCellsInSheet(ActiveSheet.Name) 
+2
source

Source: https://habr.com/ru/post/1488345/


All Articles