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.