VBA - Error handling for when file "save as" is No or Cancel

netrixuser

Board Regular
Joined
Jan 21, 2019
Messages
77
Office Version
  1. 365
Platform
  1. Windows
Hello,

Although my code works I am trying, gradually, to make it more efficient, and I am at the stage where I want to do something if, during the File "save as" command, the user does not want to overwrite an existing file or presses cancel.

My code for saving the workbook is:
Code:
        FPath = "C:\Users\" & Environ$("UserName") & "\Downloads"
        FName = Sheets("Index").Range("A104").Text
        ThisWorkbook.SaveAs Filename:=FPath & "\" & FName

In cell A104 on the Index sheet I have a concatenation of a couple of cells to produce the text "(Current Month)-(Current Year) <current month=""><current year="">Monthly MRL Reconciliation"
Today that produces Apr-2019 Monthly MRL Reconciliation and, without my intervention, it saves it with an xlsm extension :)

If, at the file save as prompt, there is already a file existing with the same name I get a popup saying as much, if No or Cancel is selected the code crashes with:



Run-time error '1004'
Method 'SaveAs' of Object'_Workbook' failed

I found a post regarding this on the site and have now added some code to stop it crashing, but I would like to be able to give the user the options of:

1) cancelling the save as and finishing the script -which is what the extra code below does
2) saying No to the overwrite question, and being prompted to change the filename - which I am struggling with tbh.​

The code to stop the script crashing when either No or Cancel is selected is

Code:
Sub Get_Reports()[INDENT]On error GoTo EH1

'My Code


[/INDENT]
[INDENT]EH1:[/INDENT]

End Sub

I now need assistance to write some "stuff" after EH1:

Any help gratefully received !!

Regards

Netrix</current></current>
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Does this help?

Code:
Option Explicit
Dim FPath As String
Dim FName As String


Sub SaveTheFile()
 FPath = "C:\Users\" & Environ$("UserName") & "\Downloads"
 FName = Sheets("Index").Range("A104").Text
 Application.EnableEvents = False
 On Error GoTo NoSave
 ThisWorkbook.SaveAs Filename:=FPath & "\" & FName
 Exit Sub
 
NoSave:
Application.EnableEvents = True
MsgBox "Cannot save as File " & FName & " already open."
On Error GoTo 0
End Sub
 
Upvote 0
Does this help?

Code:
Option Explicit
Dim FPath As String
Dim FName As String


Sub SaveTheFile()
 FPath = "C:\Users\" & Environ$("UserName") & "\Downloads"
 FName = Sheets("Index").Range("A104").Text
 Application.EnableEvents = False
 On Error GoTo NoSave
 ThisWorkbook.SaveAs Filename:=FPath & "\" & FName
 Exit Sub
 
NoSave:
Application.EnableEvents = True
MsgBox "Cannot save as File " & FName & " already open."
On Error GoTo 0
End Sub

Hi nemmi69, thank you for your reply, it works in that it will tell the user that the file already exists so cannot be saved (I changed the text slightly) without crashing. But I would prefer this to happen if Cancel was selected. If the user chose "No" to the prompt when trying to Save the File As a file that already existed, I would like them to have the option to change the name (ie be prompted to do so rather than to do something after the script has ended)

Is that doable ?

Kind regards

Netrixuser
 
Upvote 0
This better?

Code:
Option Explicit
Dim FPath As String
Dim FName As String
Dim NewFilename As String
Dim Answer As Variant


Sub SaveTheFile()
 FPath = "C:\Users\" & Environ$("UserName") & "\Downloads"
 FName = Sheets("Index").Range("A104").Text
 Application.EnableEvents = False
 On Error GoTo NoSave
 ThisWorkbook.SaveAs Filename:=FPath & "\" & FName
 Exit Sub
 
NoSave:
On Error GoTo 0
Application.EnableEvents = True
Answer = MsgBox("Cannot save as File " & FName & " already open." & _
            Chr$(13) & "Do you wish to enter a new filename?", vbYesNo, ThisWorkbook.Name)


 If Answer = vbNo Then
        'Code for No button Press
        MsgBox "You pressed NO!"
    Else
        'Code for Yes button Press
        NewFilename = InputBox("Please enter new filename", _
         "filename", "Type your filename here")
        If NewFilename <> "" Then
            FName = NewFilename
            Resume
        End If


    End If
End Sub
 
Upvote 0
Solution
Many thanks nemmi69, and apologies for taking over a year to respond !!!! (My job spec changed and I didn't get to finish the VBA project I was working on)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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