Macro File Save Error Handling

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a simple macro (below) that copies a worksheet to a new file and saves it.

I have noticed there are two problems (at least) that can cause an error:

1) There is a file already open with the same name
2) The user is not authorized to save in the current directory

I have not used "error handling" code and I wonder if anyone could help. Does each of these errors have a Microsoft error number, or something like that?

Ideally I would like a message box that explains the error to the user, then quits the macro on "OK".

Thanks,

MikeG


Sub Save_Quote()


' Save_Quote Macro
Dim wbNew As Workbook

ThisWorkbook.Worksheets("Create Or Modify A Quote").Copy

Set wbNew = ActiveWorkbook
With wbNew
.SaveAs Filename:=Range("Quote_Save_Name") & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
.Close

End With

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Maybe like this


Code:
Sub Save_Quote()


' Save_Quote Macro
Dim wbNew As Workbook
On Error GoTo ErrorTrap
ThisWorkbook.Worksheets("Create Or Modify A Quote").Copy

Set wbNew = ActiveWorkbook
With wbNew
    .SaveAs Filename:=Range("Quote_Save_Name") & ".xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    .Close

End With
Exit Sub
ErrorTrap:
MsgBox Err.Number & vbTab & Err.Description
End Sub
 
Upvote 0
Thanks VoG - exactly what I was looking for.

MikeG
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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