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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
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,231
Messages
6,170,884
Members
452,364
Latest member
springate

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