I wrote some VBA to copy a range from a worksheet and paste it as values to another worksheet in the same workbook. It failed with error 1004. I set the macro recorder running and collected the code generated when I performed the operation by hand. As a stand-alone macro, it worked without error. When I appended these lines of code unaltered to my existing macro, error 1004 again appeared. After some trial and error, I found that if I segregated the copy/paste to a subroutine and called it with the source range as a parameter, it ran without error. So, summarising:
Sheets("Contract Auth").Select
r.Select
Selection.Copy
Sheets("SupportCalculator").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
....fails with error 1004. But:
Test(r)
..runs without error, when Test is defined as:
Sub Test(r As Range)
Sheets("Contract Auth").Select
r.Select
Selection.Copy
Sheets("SupportCalculator").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
What is going on?
Bookmarks