0

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”

enter image description here

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
Community
  • 1
  • 1
Xavi
  • 207
  • 1
  • 3
  • 20

2 Answers2

2

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

SJR
  • 22,986
  • 6
  • 18
  • 26
1

The right way to do it would be

Range("M" & Firsttrow & ":M" & lastrow")