Runtime Error on execution of Save VBA

searchlight

Board Regular
Joined
Dec 25, 2009
Messages
112
I have this code to save 2 copies of my file when I close excel, one in the original folder and the other in a designated folder.

I have the file read-only and when the file is closed in the read only mode it gives a runtime error but still executes properly.

How do I get past the error so it does not show and goes directly to the normal read-only prompt?

Run-time error '1004':

'eRDL.xls' is read-only. To save a copy, click OK, then give the workbook a new name in the Save As dialog box.

Once you select End on the error message box it goes to the Save As prompt or closes.

Thanks!

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Note that any previous backup is overwritten
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs Filename:="C:\Documents and Settings\My Documents\" & _
ActiveWorkbook.Name
ActiveWorkbook.Save
Application.DisplayAlerts = True
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Is the point of all this to prompt the user for a Save As file name?

OK the error stated above is a macro run-time error which one would not normally receive unless there is a problem with a macro. The alert message that opens displays the message above and two buttons End and Help.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
When most folks see this message they think they have a problem or have screwed something up. I simply just don't want the Microsoft Visual Basic run-time error message to show. I have alerts turned off in the code but I guess this doesn't suppress macro error alerts.
 
Upvote 0
This line in your macro will try to re-save your current read-only file. This causes the error.
Code:
ActiveWorkbook.Save

But then you said you don't want to re-save your read-only file. If you don't want to re-save your file and you don't want the error message, then remove that line from your code.

If you really want to ignore macro errors, then use something like this to suspend error checking.
Code:
On Error Resume Next
But in most cases, it's better to handle the errror rather than ignore it.

To resume error checking...
Code:
On Error Goto 0

Error Handling In VBA
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

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