Reputation: 207
I have a macro which selects my first cell used in column M (variable is Firstrow). This macro detects the last cell used in column M (variable is lastrow).
I would like that my macro selects from first cell used in column M to last cell used in column M.
I tried:
Range (Firsttrow & "M:M" & lastrow)
but I got an error message
“Compile error: invalid use of property”
Sub Selectfromfirstrowusedtolastrowusedused()
Dim lastrow, Firstrow As Long
lastrow = ActiveSheet.Range("M" & Rows.Count).End(xlUp).Row
Firstrow = Range("M1").End(xlDown).Row
Debug.Print Firstrow
Debug.Print lastrow
Range (Firsttrow & "M:M" & lastrow)
End Sub
Upvotes: 0
Views: 2608
Reputation: 23081
You need
Range("M" & Firsttrow & ":M" & lastrow).Select
because it is column then row using Range. An alternative would be
Range(cells(firstrow,"M"),cells(lastrow,"M")).Select
Not that you rarely need to select anything though.
Also, in this line
Dim lastrow, Firstrow As Long
lastrow is declared as a variant so better to do
Dim lastrow As Long, Firstrow As Long
Upvotes: 2
Reputation: 31
The right way to do it would be
Range("M" & Firsttrow & ":M" & lastrow")
Upvotes: 1