简体   繁体   中英

Select the range from first cell used to last cell used in column

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

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

正确的方法是

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM