You are currently offline, waiting for your internet to reconnect

How to Avoid "Save Changes?" When You Close a Workbook

This article was previously published under Q129153
This article has been archived. It is offered "as is" and will no longer be updated.
In Microsoft Excel, you can create a Microsoft Visual Basic forApplications macro that suppresses the "Save Changes?" prompt when youclose a workbook. You can do this either by specifying the state of theworkbook's Saved property or by suppressing all alerts for the workbook.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. Before working with each example below, perform the following steps:

  1. Open a new workbook.
  2. Insert a new module sheet:

    • In Microsoft Excel 97 or Microsoft Excel 98, point to Macro on the Tools menu, and then click Visual Basic Editor. In the Editor, click Module on the Insert menu.
    • In Microsoft Excel versions 5.0 and 7.0, point to Macro on the Insert menu, and then click Module.
  3. Type the sample macro code into the module sheet.
The Saved property returns the value False if changes have been made to aworkbook since it was last saved.

You can use the reserved subroutine name "Auto_Close" to specify a macrothat should run whenever a workbook is closed. By doing this, you cancontrol how the document is handled when the user is finished and hasinstructed Microsoft Excel to close it.

Example 1: Closing the Workbook Without Saving Changes

To force a workbook to close without saving any changes, use the followingcode in a Visual Basic module sheet of that workbook:
   Sub Auto_Close()       ThisWorkbook.Saved = True   End Sub				
Because the Saved property is set to True, Microsoft Excel thinks theworkbook has already been saved and that no changes have occurred sincethat last save.

The DisplayAlerts property of the application can be used for the samepurpose. For example, the following macro turns DisplayAlerts off,closes the active workbook without saving changes, and then turnsDisplayAlerts on again.
   Sub CloseBook()       Application.DisplayAlerts = False       ActiveWorkbook.Close       Application.DisplayAlerts = True   End Sub				
You can also do this by using the SaveChanges argument of the Closemethod.

The following macro closes the workbook without saving changes:
   Sub CloseBook2()       ActiveWorkbook.Close savechanges:=False   End Sub				

Example 2: Closing the Workbook and Saving the Changes

To force a workbook to save changes, use the following code on a modulesheet of that workbook:
   Sub Auto_Close()       If ThisWorkbook.Saved = False Then           ThisWorkbook.Save       End If   End Sub				
This subroutine checks to see if the file's Saved property is False. Ifso, then the workbook has been changed since the last save, and it savesthose changes.
5.00a 5.00c 8.00 Mac XL5 XL7 XL97 XL98 dialog Activeworkbook.SaveAs save as saveas XL

Article ID: 129153 - Last Review: 12/04/2015 11:16:20 - Revision: 2.4

Microsoft Excel 5.0c, Microsoft Excel 5.0 Standard Edition, Microsoft Excel 95a, Microsoft Excel 5.0 for Macintosh, Microsoft Excel 5.0a for Macintosh, Microsoft Excel 5.0 for Macintosh, Microsoft Excel 5.0a for Macintosh, Microsoft Excel 97 Standard Edition, Microsoft Excel 95 Standard Edition, Microsoft Excel 5.0 Standard Edition, Microsoft Excel 98 for Macintosh

  • kbnosurvey kbarchive kbdtacode kbhowto kbprogramming KB129153