Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Saving Changes when Closing.

Saving Changes when Closing

by Allen Wyatt
(last updated July 15, 2019)

3

If you modify an Excel workbook, and then close the file, you are asked if you want to save your changes. This is a good feature that helps ensure you don't mistakenly throw away some of your work.

When running a macro, however, you may not want to be bothered with a dialog box asking if you want to save your changes. If the macro modifies a workbook in some way, and you use the Close method, you are asked if you want to save your changes, just as you are if you manually close a workbook without first saving.

The way to get around this is to use one of the parameters available with the Close method. Consider the following:

    ActiveDocument.Close SaveChanges:=False
    ActiveWorkbook.Close SaveChanges:=True

Both lines of code close the active worbook. The difference between the lines is in the setting of the SaveChanges parameter. In the case of the first line, any changes will be discarded, while the second line results in the workbook being saved when it is closed.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10674) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Saving Changes when Closing.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

An Automatic File Name

Do you have a set "standard" for how you name new documents? If so, you may be interested in implementing the technique ...

Discover More

Using the Fill Handle from the Keyboard

The fill handle can save a huge amount of time when you are editing a worksheet. If you are really good at using the ...

Discover More

Creating a Year-to-Date Comparison Chart

Excel is an excellent tool for keeping track of data over time. If you have information you are keeping by year, you may ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Running a Macro in a Number of Workbooks

Got a macro that you need to run on each of a number of workbooks? Excel provides a number of ways to go about this task, ...

Discover More

Removing Pictures for a Worksheet in VBA

Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...

Discover More

Converting HSL to RGB

When working with colors in Excel you can specify them using either RGB or HSL values. Converting from HSL to RGB can be ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 5?

2019-07-16 13:42:40

Martin Shingler

I use some workbooks to collect, analyse and report on data regularly. Their macros collect data from specific folders and save reports to other locations. At the end, as the workbook is closed, I use

ThisWorkbook.Saved = True

in the Workbook_beforeClose macro for worksheets where I want to control the version and offer the next user a "clean sheet" for processing their data.

One to watch out for is during development, (or enhancement) you have to ensure that you comment this out, so that if you forget to save your changes, Excel will prompt you to save the modified file.


2019-07-16 13:36:51

Martin Shingler

I use some workbooks to collect, analyse and report on data regularly. Their macros collect data from specific folders and save reports to other locations. At the end, as the workbook is closed, I use

ThisWorkbook.Saved = True

in the Workbook_beforeClose macro for worksheets where I want to control the version and offer the next user a "clean sheet" for processing their data.

One to watch out for is during development, (or enhancement) you have to ensure that you comment this out, so that if you forget to save your changes, Excel will prompt you to save the modified file.


2019-07-15 05:54:49

Willy Vanhaelen

ActiveDocument.Close SaveChanges:=False

This is code for a Word document and won't work in Excel.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.