Socii
Socii

Reputation: 545

Difference between:- Range(Cells(2,3),Cells(lastRow,3)) and Range("C2:C23")?

I have a line of vba in which I'm trying to set a Range object referencing a Range on a protected Worksheet. It works fine if I use the following line:

Set rngList = wsTemp.Range("C2:C23")

But for some reason it breaks with a "Method 'Range' of object 'worksheet' failed" error if I use a similar line:

Set rngList = weTemp.Range(Cells(2, 3), Cells(lastRow, 3))

lastRow is an integer and = 23, so both lines should work equally?

Upvotes: 1

Views: 397

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149305

That is because your cells object is not fully qualified. Try this

Set rngList = weTemp.Range(weTemp.Cells(2, 3), weTemp.Cells(lastRow, 3))

or

With weTemp
    Set rngList = .Range(.Cells(2, 3), .Cells(lastRow, 3))
End With

Upvotes: 1

Related Questions