sharky12345
Well-known Member
- Joined
- Aug 5, 2010
- Messages
- 3,422
- Office Version
- 2016
- Platform
- 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;
And the line that fails is this one;
Can anyone give me a clue what's going on?
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?