Error 1004 when saving copied worksheets as new workbook

becci.gott

Board Regular
Joined
Oct 2, 2012
Messages
79
I am automating the production of a load of output reports, and was getting a 400 Error when I tried to run. I have now narrowed it down and through the err.description have identified it as a 1004 error "The cell or chart you're trying to change is on a protected sheet. To make a change, unprotect the sheet. You might be requested to enter a password."

It specifically happens at the point of saving the copied sheets as the output filename. It seems to save without any problem, but then fails before it moves onto the next command (I did put a debug.print command in after the saveas, but it never got as far that).

I have even opened the saved file, and it exists, so I can't figure out how the hell to make this work!

Code:
'Set output path
strFilepath = ws.Range("FCSaveFilePath").Value & ws.Range("DateSubFolder").Value
strFilename = "Forecast Model - " & ws.Range("FileSuffix").Value


'Change strfilename incase of #N/A
strFilename = Replace(strFilename, "#N/A", "Client_Name Error")
filename = strFilepath & strFilename

'Copy report output sheets into new workbook
With Sheets(Array("Forecast Rolling 12 Months", "Report Rolling 12 Months", _
    "Balance Sheet Assumptions", "3 Way Summary Forecast"))
        .Select
        .Copy
End With


Set nwb = ActiveWorkbook

'...Does some other bits in here that run without any issues...


'Save output report
With nwb    
    .SaveAs filename:=filename, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    .Close
End With

I have used this exact same code in different macros (I have about 6 different workbooks that generate output reports), and it is only this one that is resulting in an error.

Edited to add that it works without issue if I F8 through the commands.

Any help would be greatly appreciated.
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I would be tempted to add DoEvents before saving
 
Upvote 0
I would be tempted to add DoEvents before saving

I did try that without much luck.

I have done something that makes it work, but I can't honestly say that I like it..

I added in an "on error goto ErrorCatche"

And then in the error catcher had a wait for 5 second and resume next.. It ain't pretty, but it does get the job done.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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