Save as macro returns error when the user hits cancel on the save as box

morrisps

New Member
Joined
Jul 17, 2017
Messages
15
I have the following piece of cod, which is designed to obtain a file name and then save the excel file with the name. It works great until the user hits cancel. It then over saves the current file with the name false or returns the debugger. I want it to simply end if the user hits cancel but cannot find a way to do that.


Code:
Sub SaveFile()
' If the locations of the cells containing the names changes please simply redefine the ranges attached to the variables
Sheets("Info & Readings").Select

UPSName = Range("A18")
MtnceType = Range("AB7") 'maintaince type, Major or minor
'Address:     'Broken up into Street number, street, city, and province
    StNumb = Range("J9")
    StName = Range("N9")
    City = Range("T9")
    Prov = Range("Y9")
MtnceWorkDate = Range("T4")

If (UPSName = 0) Or (MtnceType = 0) Or (MtnceWorkDate = 0) Then
        MsgBox ("Please fill in required fields first: UPS Identification Name, Mtnce. Type, Address, and Mtnce. Work Date. Thank You")
    Else
       If (StNumb = "") Or (StName = "") Or (City = "") Or (Prov = "") Then
            MsgBox ("Please fill in required fields first: UPS Identification Name, Mtnce. Type, Address, and Mtnce. Work Date. Thank You")
        Else
            NomFichier = UPSName & " " & MtnceType & " " & " " & StNumb & " " & StName & " " & City & " " & Prov & " " & Format(Range("T4").Value, " yyyy-mmm-dd") & " .xls"
            ActiveWorkbook.SaveAs Filename:=Application.GetSaveAsFilename(NomFichier, FileFilter:="Fichier (*.xls), *.xls")
        End If
    End If

End Sub
 

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).
Save it in a variable so that you can test the value before you execute the SaveAs

Code:
x = Application.GetSaveAsFilename(NomFichier, FileFilter:="Fichier (*.xls), *.xls")
If x <> False Then ActiveWorkbook.SaveAs Filename:=x
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,451
Members
449,161
Latest member
NHOJ

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