Range Only Works When Called from the Referenced Worksheet [duplicate]

女生的网名这么多〃 提交于 2019-11-28 06:33:07

问题


VBA/Programming newbie here! I've got a macro assigned to two buttons, each on a different worksheet: "Print Page 1" and "Optimization". The macro takes information from the first to know how many times to copy a range the second.

When called while "Optimization" is active, the code works perfectly, but when called with the "Print Page 1" worksheet active, a "1004 error: Application-defined or object-defined error" is produced on the line that copies the ranges. See below:

Sub Range_Tester()

Dim PrintRow, WallQty

WallQty = Worksheets("Print Page 1").Cells(24, 4)
PrintRow = 20

While WallQty > 1
    Worksheets("Optimization").Range(Cells(PrintRow - 1, 2), Cells(PrintRow - 1, 4)).Copy Worksheets("Optimization").Range(Cells(PrintRow, 2), Cells(PrintRow, 4))
    WallQty = WallQty - 1
    PrintRow = PrintRow + 1
Wend

End Sub

I think the problem is my range syntax; if I replace

     Worksheets("Optimization").Range(Cells(PrintRow - 1, 2), Cells(PrintRow - 1, 4)).Copy Worksheets("Optimization").Range(Cells(PrintRow, 2), Cells(PrintRow, 4))

with

    Worksheets("Optimization").Cells(PrintRow - 1, 2).Copy Worksheets("Optimization").Cells(PrintRow, 2)
    Worksheets("Optimization").Cells(PrintRow - 1, 3).Copy Worksheets("Optimization").Cells(PrintRow, 3)
    Worksheets("Optimization").Cells(PrintRow - 1, 4).Copy Worksheets("Optimization").Cells(PrintRow, 4)

The info copies flawlessly regardless of which worksheet is active.

My apologies for asking what is probably a stupidly simple question, I just haven't had any luck finding info on properly defining ranges that change locations.


回答1:


Cells being unqualified, implicitly refers to ActiveSheet, because that code isn't written in a worksheet module. If it was, then unqualified Cells would be like Me.Cells. Anywhere else, it's ActiveSheet.Cells.

Always explicitly qualify Worksheet member calls with a proper Worksheet object, and you'll avoid this problem.

Worksheets("Optimization").Range(Cells(PrintRow - 1, 2), Cells(PrintRow - 1, 4)).Copy Worksheets("Optimization").Range(Cells(PrintRow, 2), Cells(PrintRow, 4))

Here, you're invoking .Range against Worksheets("Optimization"), but then parameterizing it with Cells(PrintRow - 1, 2), which may belong to another sheet (whatever ActiveSheet is). When it does, things blow up.

Capture the Worksheets("Optimization") reference into a With block variable (or a plain old local variable):

With Worksheets("Optimization")
    .Range(.Cells(PrintRow - 1, 2), .Cells(PrintRow - 1, 4)).Copy .Range(.Cells(PrintRow, 2), .Cells(PrintRow, 4))
End With

Notice the . dereferencing operator, which makes .Cells work off Worksheets("Optimization") like the .Range call, ensuring everything is pulled from the same sheet.



来源:https://stackoverflow.com/questions/53599410/range-only-works-when-called-from-the-referenced-worksheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!