Error 1004 - Copy method of Worksheet class failed

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

I'm having an issue trying to copy sheets to a new workbook - this works the first time I run it but fails every time after that, showing Error 1004 - Copy method of Worksheet class failed.

The full code is here;

VBA Code:
Sub SaveFile()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Sheets("DATA").Activate
Sheets("SUMMARY").Activate

Set sourceWs = ThisWorkbook.Sheets("SUMMARY")
'Copy the source worksheet to a new workbook
sourceWs.Copy
'Get a reference to the new workbook
Set newWb = ActiveWorkbook

ThisWorkbook.Sheets("DATA").Copy After:=newWb.Sheets("SUMMARY")
newWb.Sheets("SUMMARY").Activate

SaveFolder = ThisWorkbook.Path
ActiveWorkbook.SaveAs Filename:=SaveFolder & "\" & "Fail Data_" & Format(Now, "dd_mm_yy") & ".xlsx", FileFormat:=51
                                   
FilePath = Thisworkbook.Path
ActiveWorkbook.SaveAs FilePath & "\" & "Fail Data_" & Format(Now, "dd_mm_yy") & ".xlsx", FileFormat:=51

newWb.Save
newWb.Close False
Set newWb = Nothing

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

And the line that fails is this one;

VBA Code:
ThisWorkbook.Sheets("DATA").Copy After:=newWb.Sheets("SUMMARY")

Can anyone give me a clue what's going on?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
To fix error 1004 in Excel, try checking and correcting any VBA code mishaps, removing problematic add-ins, or deleting the 'GWXL97. XLA' file. If those don't work, creating a fresh Excel template or using a file repair tool could do the trick.
 
Upvote 0

Forum statistics

Threads
1,224,735
Messages
6,180,635
Members
452,991
Latest member
JM_000888

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top