0
votes

The error appears on the line before end sub, what is the correct way to select the range with the last row calculated?

Sub My_Script()
Dim LastRow As Long
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    End With
  ActiveSheet.Range(Cells(LastRow, Columns("D:AH"))).Select
End Sub
1

1 Answers

1
votes

The code below select all cells in the LastRow only , from Column "D" to Column "AH".

Sub My_Script()

Dim LastRow As Long

With ActiveSheet
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    .Range(.Range("D" & LastRow), .Range("AH" & LastRow)).Select
End With

End Sub

If you wanted to select all rows (from row 1 to LastRow), from Column "D" to Column "AH".

Sub My_Script()

Dim LastRow As Long

With ActiveSheet
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    .Range(.Range("D1"), .Range("AH" & LastRow)).Select
End With

End Sub

Note: not sure why you want to Select this range ? You can set this Range to a variable of type Range, and later to Copy or whatever you want to do with it. There's hardly a reason to ever use Select.