VBA SaveAs with vbOKCancel

GeorgeWhite

New Member
Joined
Apr 20, 2017
Messages
27
Hello

I am looking for a way I can open a SaveAs dialog using VBA, then if the user hits cancel it will show a msgbox with "You need to save the document before you can continue." on a vbOKCancel + vbCritical popup. If the user then selects cancel it will close the workbook but if they select ok it will bring the SaveAs dialog back up but I then want it too keep repeating it, if they select cancel again it will repeat the steps. If they select ok and successfully save the file it will continue to the next part of my code which is just activating another workbook and closing it.

I currently have this but I am willing to use something else.
Code:
Dim bFileSaveAs As Boolean
bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
If Not bFileSaveAs Then MsgBox "You need to save the document before you can continue.", vbOKCancel + vbCritical
ActiveWorkbook.Close savechanges:=False

Many thanks
 
Last edited:

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.
Hi, maybe you could try something like this.

Code:
Dim bFileSaveAs As Boolean
Do
  bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
  If Not bFileSaveAs Then
    If MsgBox("You need to save the document before you can continue.", vbOKCancel + vbCritical) = vbCancel Then
      ActiveWorkbook.Close savechanges:=False
      Exit Sub
    End If
  End If
Loop Until bFileSaveAs = True
 
Upvote 0
Hi, maybe you could try something like this.

Code:
Dim bFileSaveAs As Boolean
Do
  bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
  If Not bFileSaveAs Then
    If MsgBox("You need to save the document before you can continue.", vbOKCancel + vbCritical) = vbCancel Then
      ActiveWorkbook.Close savechanges:=False
      Exit Sub
    End If
  End If
Loop Until bFileSaveAs = True
Hi, this works perfect thanks so much!!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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