Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Preserving the Undo List.

Preserving the Undo List

Written by Allen Wyatt (last updated January 9, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


As you are working in Excel, there is a bit of a "safety net" in place in regards to changes you make. Most people know that if they mess things up, they can quickly press Ctrl+Z or choose the Undo command from the Edit menu or by using the Undo tool on the toolbar. If you don't like what you just did, you can easily undo it and get back to the way things were before.

When you run a macro, however, the macro doesn't "play nice" with the Undo list. In fact, running a macro completely erases the Undo list, and therefore you cannot automatically undo the effects of running the macro. There is no intrinsic command—in Excel or in VBA—to preserve the Undo list. There are a couple of ways that you can approach the problem, however.

If you feel that you might want to undo the effects of a macro, the first thing you can do is to save your workbook before running the macro. This, in effect, gives you a "pre-macro" version of the workbook. If you want to later revert to this version, simply close the workbook without saving and then reload it from disk.

Another option is to rethink the way you do your macros. If you have a macro that does a lot of processing of information in your worksheet, code the macro so that it maintains, in memory, the state of anything that it changes. You can then create a separate macro that reads this information and effectively undoes the effects of the first macro.

To make this approach really handy, the last step in your primary macro can be to "stuff" information on the Undo stack. This info can then be used, but the user, to "undo" macro that you created. For instance, the following macro command could be the last one in your primary macro:

Application.OnUndo "Primary Macro", "UndoPrimary"

After this command, when the user looks at the Undo list, he or she will see the text "Primary Macro." If they choose this option from the Undo list, then your "undo" macro (UndoPrimary) is executed.

You should note that this approach doesn't save what was on the Undo list before you ran the macro—there seems no way to do that. When your primary macro is through running, there will only be a single option available on the Undo list: Primary Macro.

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 (2060) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Preserving the Undo List.

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

Inserting Large Numbers of Checkboxes

Excel provides a number of tools you can use to help create forms. One of those tools is a checkbox. If you need to place ...

Discover More

Switching between Program Windows

It is not unusual for you to work with several (and sometimes many) programs at the same time. Each program is displayed ...

Discover More

Converting UTC Times to Local Times

Dates and times are often standardized on UTC time, which is analogous to GMT times. How to convert such times to your ...

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 (menu)

Counting Cells with Text Colors

Got a bunch of cells that have different colored text in them? Here's a great way to count the occurrences of certain ...

Discover More

Controlling Display of Toolbar Buttons

You can assign your macros to a series of custom toolbar buttons, but you may only want those buttons to be visible when ...

Discover More

Combinations for Members in Meetings

Got a large group of people listed in a worksheet and you want to make sure that each person has met with every other ...

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}] (all 7 characters, in the sequence shown) 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 nine minus 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.