Don't save when user closes workbook

gustoaf

New Member
Joined
Sep 23, 2008
Messages
4
Is there a line of code that I could throw into my workbook that will not save any changes when the user closes the file?

I have a shell spreadsheet that runs a macro, populates the sheets with external data, and changes the names of the sheets. The data that is populated is for visual reference only.

When the user closes the app, I want the workbook to revert to its "shell" state, with no dialog boxes asking if the user wants to save the file. Just X out and not save.

Thanks.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Right click the excel icon to the left of the File menu, View Code, and paste this.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  ThisWorkbook.Close False
End Sub
Of course if they save the file and then close it, this alone won't do it for you.
 
Upvote 0
Similar to Kenneth:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close savechanges:=False
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True
End Sub

Whilst still in the code window press CTRL + G to open the Immediate window, type in

Code:
Application.EnableEvents = False

and press Enter. This will prevent the above code from running. Then save the file. Reopen the file, press ALT + F11 to open the VBE, press CTRL + G and type in

Code:
Application.EnableEvents = True

The file now should not be savable and shouldn't give any warning when closed.
 
Upvote 0
This eliminates the 'Do you want to save changes?' message but Excel itself is still open. Is there a way to make Excel go away so the application shutdown is seamless without dialogs?
 
Upvote 0
Try

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close savechanges:=False
Application.Quit
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,422
Messages
6,119,396
Members
448,891
Latest member
tpierce

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