Exit Sub if document is not saved

tobysmith69

New Member
Joined
Aug 28, 2015
Messages
29
Hello all,

I am new to this forum, and a complete and utter beginner at VBA. Using guides from various forums on the internet, I have produced the below sub. It prompts a person to Save As the excel spreadsheet they are in, before calling on another sub which attaches the newly saved spreadsheet to an e-mail. This all works absolutely fine, however when prompted to save the document, if they click Cancel and don't save it, it will still carry out the second sub. I want it to exit the sub and not continue if they haven't saved the document, but I can't for the life of me work out how to do this. Any help gratefully received!

Toby

Sub mcrSave()

'Retrieve file name to use for Save
fileSaveName = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls")

'If user specified file name, perform Save and display msgbox
If fileSaveName <> False Then
ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=xlNormal

End If

Call EmailOrder

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.
Hi and welcome to the forum,

Code:
response = MsgBox("Do you want to save and send the file?", vbYesNo)
If response = vbNo Then Exit Sub

[COLOR=#008000]'Code here​ Will run only if user click yes[/COLOR]
 
Last edited:
Upvote 0
if they click Cancel and don't save it, it will still carry out the second sub. I want it to exit the sub and not continue if they haven't saved the document
Toby,
Try moving "Call EmailOrder" inside the If Statement.
Code:
Sub mcrSave()

    'Retrieve file name to use for Save
    fileSaveName = Application.GetSaveAsFilename(fileFilter:="Excel Files (*.xls), *.xls")
    
    'If user specified file name, perform Save and display msgbox
    If fileSaveName <> False Then
        ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=xlNormal
        Call EmailOrder
    End If


End Sub
You can also add "Exit Sub" inside the If statement after the "ActiveWorkbook.SaveAs" line if you need to keep "Call EmailOrder" outside. Hope this helps!
 
Upvote 0
Thank you to you both for your help. I have moved Call EmailOrder inside the If statement, and it now works perfectly.
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,823
Members
449,470
Latest member
Subhash Chand

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