Unable to Suppress Save Prompt

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
Hello Everyone,
I am trying to prevent the save prompt when the 'X' at the top right of the sheet is used to close a file. I have all the usual stuff and I have been trying to embellish it with extra event enabling and extra Alerts = False etc, but nothing will prevent the prompt if the X is used. The following code is situated in Excel Objects: "ThisWorkbook". Should I put this code onto each sheet? There are several in this particular workbook. Any help would be greatly appreciated.
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.DisplayAlerts = False
Application.EnableEvents = True
ThisWorkbook.Saved = True
Application.DisplayAlerts = True
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.EnableEvents = True
Application.DisplayAlerts = False
ThisWorkbook.Saved = True
Cancel = True
Application.DisplayAlerts = True
End Sub
Thanks in advance,
David
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I cannot reproduce your problem. When I used your code, change the file, then X out, it closes without a prompt.

Also, if you are cancelling the save any time the user tries to save it, then Saved is going to be True and you don't need to avert a prompt with BeforeClose. Yet, somehow you are getting a prompt anyway. I don't know why.

If you move this code anywhere else it will have no effect at all. It will be called only if it is in ThisWorkbook.

Are you absolutely certain that
The following code is situated in Excel Objects: "ThisWorkbook".
 
Upvote 0
I tried you code. No prompt for me 3x. However, changes are not saved. Not sure if that is your intent.
 
Upvote 0
I cannot reproduce your problem. When I used your code, change the file, then X out, it closes without a prompt.

Also, if you are cancelling the save any time the user tries to save it, then Saved is going to be True and you don't need to avert a prompt with BeforeClose. Yet, somehow you are getting a prompt anyway. I don't know why.

If you move this code anywhere else it will have no effect at all. It will be called only if it is in ThisWorkbook.

Are you absolutely certain that
Yes, absolutely certain. I am starting to think there may be some corruption of the file that is causing this not to work. It is an old file that I have been working on for years. I might just start fresh and import all the sheets into a new file. I did this once before and it solved a different issue with a different program.
 
Upvote 0
I tried you code. No prompt for me 3x. However, changes are not saved. Not sure if that is your intent.
Yes, my intent is to not save any changes. I want the file to respond to new data being pasted in each time it is accessed.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,143
Members
453,021
Latest member
Justyna P

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