Save as MarcoEnabled Workbook - Still saving on 'cancel'

eli_m

Board Regular
Joined
Jun 2, 2022
Messages
153
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have this code

VBA Code:
Sub SaveQuoteAsWorkbook()

Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats

Dim tDate As String
Dim FileSaveName As String
Dim fName As String

fName = Range("H4") & "" & Format(today, "DD-MM-YYYY")
FileSaveName = Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file")
ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52

End Sub

1. Run the Macro
2. Dialogue pops up to save:
1678171187006.png


3. I select 'Cancel'
4. and still it saves the Macro Enabled Template as 'False.xlsm'

How can I stop it from actually saving? I thought 'Cancel' would stop it.

Thanks in advance :)
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try...
Rich (BB code):
FileSaveName = Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file")
If FileSaveName = False Then Exit Sub

Declare FileSaveName as Variant
 
Upvote 0
Solution
@MARK858 the OP will need to change the Dim FileSaveName to Variant (currently String) or is will give a type mismatch.

The below seemed to work as well if you wanted to keep FileSaveName as String
Rich (BB code):
Sub SaveQuoteAsWorkbook()

Range("H4").Copy
Range("H4").PasteSpecial xlPasteValuesAndNumberFormats

Dim tDate As String
Dim FileSaveName As String
Dim fName As String

fName = Range("H4") & "" & Format(Date, "DD-MM-YYYY")
FileSaveName = CStr(Application.GetSaveAsFilename(InitialFileName:=fName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file"))
If FileSaveName <> "False" Then ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52

End Sub
 
Upvote 1
Ok guys, I have an additional part to the puzzle for you:

If I run my macro to save and I hit cancel it exits the sub ✔️
If I run my macro to save and I hit Save.....it still exits the sub.

Is there a way to have it continue through the sub if you click save but to exit if you click cancel?
 
Upvote 0
You need to show us the actual code you are using, preferably by inserting it here using either the VBA or RICH button.
 
Upvote 0
Sorry about that - here is the code. I need it to continue to clear the cells and then save it a second time if the user saves the first document but to exit the code and not clear/save if they cancel the first save.

VBA Code:
MPID = Range("r3").Text
tday = Range("ac4").Text
inspect = Range("E6").Text
ENO = Range("AC3").Text
tmr = Range("T237").Text
tdayName = "DIR - " & MPID & " - " & tday & " - " & inspect
tmrName = "DIR - " & MPID & " - " & tmr & " - " & inspect

MsgBox "Click YES when prompted to save over an existing file"
Application.Dialogs(xlDialogSaveAs).Show tdayName
ThisWorkbook.Save
If FileSaveName = False Then Exit Sub

'FIRST SECTION
   Range("g7:m7").ClearContents
   Range("e8:g8").ClearContents
   Range("k8:m8").ClearContents
   Range("f10:I10").ClearContents
   Range("r7:w7").ClearContents
   Range("r8:w8").ClearContents
   Range("o10:r10").ClearContents

Application.Dialogs(xlDialogSaveAs).Show tmrName

'LAST CLEAR, SAVE AND CLOSE
    Range("ac4:AJ4").ClearContents

ThisWorkbook.Save
 
Upvote 0
I have not been able to test this but see if the below works for you.
Since you are trying to capture cancelling the Save, I wouldn't use the pplication.Dialogs(xlDialogSaveAs)

Rich (BB code):
MPID = Range("r3").Text
tday = Range("ac4").Text
Inspect = Range("E6").Text
ENO = Range("AC3").Text
tmr = Range("T237").Text
tdayName = "DIR - " & MPID & " - " & tday & " - " & Inspect
tmrName = "DIR - " & MPID & " - " & tmr & " - " & Inspect

MsgBox "Click YES when prompted to save over an existing file"

'Application.Dialogs(xlDialogSaveAs).Show tdayName
Dim FileSaveName As Variant
FileSaveName = Application.GetSaveAsFilename(InitialFileName:=tdayName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file")
If FileSaveName <> False Then
    ThisWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52
End If

'FIRST SECTION
   Range("g7:m7").ClearContents
   Range("e8:g8").ClearContents
   Range("k8:m8").ClearContents
   Range("f10:I10").ClearContents
   Range("r7:w7").ClearContents
   Range("r8:w8").ClearContents
   Range("o10:r10").ClearContents

'Application.Dialogs(xlDialogSaveAs).Show tmrName
'LAST CLEAR, SAVE AND CLOSE
    Range("ac4:AJ4").ClearContents
    
    FileSaveName = Application.GetSaveAsFilename(InitialFileName:=tmrName, filefilter:="Excel Files(*.xlsm),*.xlsm", Title:="Please save the file")
    If FileSaveName <> False Then
        ThisWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=52
    Else
        Exit Sub
    End If
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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