Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. 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 February 1, 2020)

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:

    ActiveWorkbook.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, 2013, 2016, 2019, and Excel in Office 365. 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

Determining the Current Directory

When you use a macro to do file operations, it works (by default) within the current directory. If you want to know which ...

Discover More

Saving Money on Printing Labels

Labels can be expensive, and a little common sense will help you waste less money as you try to get your labels to appear ...

Discover More

Printing Only Selected Pages

I often need to print only select pages of a document, rather than the whole thing. Word makes it easy to be judicious in ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Macro Runs Slowly, but Steps Quickly

When you have a macro that processes a huge amount of data, it can seem like it takes forever to finish up. These ...

Discover More

Magnifying Only the Current Cell

You can use the Zoom feature of Excel to magnify what Excel shows of your workbook, but it affects the entire screen. ...

Discover More

Creating a Floating Macro Button

Macros can make your use of Excel much more powerful. If you have a macro that is triggered by an on-screen button, you ...

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 4 + 0?

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.