简体   繁体   中英

Excel VBA range copy from another sheet

I have a line of code that I am having issues troubleshooting.

Sheets("data").Range(cells(2,1), cells(2, column).Copy

I'm getting a 1004 application or object defined error on this. The code works if I remove the worksheet selection or replace the range chunk with a hardcoded reference (e4:e50 for example), but won't work together.

Right now you have the Range looking at one sheet and the Cells are looking at the active sheet. You need to make sure they are looking at the same sheet.

You can do this in line:

Sheets ("data").Range(Sheets ("data").cells (2,1), Sheets ("data").cells (2, column)). Copy

Or you can use a With Block for less typing

With Sheets ("data")
    .Range(Sheets (.cells (2,1), .cells (2, column)). Copy
End With

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