简体   繁体   English

VBA复制范围错误1004

[英]VBA copy range error 1004

I get a 1004 error when running this code: 运行此代码时出现1004错误:

Dim Row As Integer
Dim Col As Integer

Row = Worksheets("Design").Cells(11, 22).Value
Col = Worksheets("Design").Cells(12, 22).Value

Worksheets("Tablecorrected").Range(Cells(2 + 19 * Row, 1 + 19 * Col), Cells(19 + 19 * Row, 18 + Col * 19)).Copy _
Destination:=Worksheets("Scriptsheet").Range(Cells(1, 1), Cells(18, 18))

It point to the copy line, and I dunno what is wrong here. 它指向复制线,我不知道这里有什么问题。 Thanks for you help 谢谢你的帮助

The Cells() inside the Range() are referring to the active sheet not the sheet to which the Range() is referring. Cells()的内部Range()指的是活性不片的片材到的Range()所指。

You need to qualify the Cells() to the proper sheet. 您需要将Cells()限定为正确的工作表。

Worksheets("Tablecorrected").Range(Worksheets("Tablecorrected").Cells(2 + 19 ... and so on.

Or to save typing you would use a With Block 或者为了保存输入,您将使用With Block

Dim Row As Integer
Dim Col As Integer

Row = Worksheets("Design").Cells(11, 22).Value
Col = Worksheets("Design").Cells(12, 22).Value

With Worksheets("Tablecorrected")

    .Range(.Cells(2 + 19 * Row, 1 + 19 * Col), .Cells(19 + 19 * Row, 18 + Col * 19)).Copy _
    Destination:=Worksheets("Scriptsheet").Range(Worksheets("Scriptsheet").Cells(1, 1), Worksheets("Scriptsheet").Cells(18, 18))

End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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