Error When Using Range(Cells()) to Paste in Excel

Hi,

I get a "Method Range of Object Worksheet Failed" when I run code below. The error points to the last row of code. What I am trying to do is determine the number of the last row in Column A that has data, and then copy some cells with formulas in them from row 2 of a couple of columns and then paste them down as far as the last data in column A.

'shtCurrent_Portfolio.Activate
LastRow = shtCurrent_Portfolio.Range("A:A").Find("*", Searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
shtCurrent_Portfolio.Range("Z2:AE2").Copy
shtCurrent_Portfolio.Range(Cells(3, 26), Cells(LastRow, 31)).PasteSpecial (xlPasteAll)

I have found that if the sheet to which I am pasting is active I do not get an error, but I dont like having to activate sheets in my code. It makes me nervous :).

Any help would be appreciated.

Joe


Answer this question

Error When Using Range(Cells()) to Paste in Excel

  • nghianghesi

    Hi,

    You need to fully qualify the Cells references.

    shtCurrent_Portfolio.Range(shtCurrent_Portfolio.Cells(3, 26), shtCurrent_Portfolio.Cells(LastRow, 31)).PasteSpecial (xlPasteAll)



  • MMBVN

    Thanks Andy. Looks like a good candidate for with...end.

  • Error When Using Range(Cells()) to Paste in Excel