[RESOLVED] Save as macro returns error when the user hits cancel on the save as box
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
Re: Save as macro returns error when the user hits cancel on the save as box
Quote:
ActiveWorkbook.SaveAs Filename:=Application.GetSaveAsFilename(NomFichier, FileFilter:="Fichier (*.xls), *.xls")
you will need to break this into separate parts so you can test if the user pressed cancel
Code:
fname = Application.GetSaveAsFilename(NomFichier, FileFilter:="Fichier (*.xls), *.xls")
if not fname = false then ActiveWorkbook.SaveAs Filename:= fname
define fname as required
Re: Save as macro returns error when the user hits cancel on the save as box
Thank you, this worked great.