MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Can you undo a macro?


Posted by Tim Johnson on January 02, 2002 12:04 PM

I have several macros that I use all the time. When I accidentally run the wrong one, I cannot "undo" them. Is there some code I can insert into each macro that will allow me to "undo" the macro when it is run the 2nd time?


Posted by Jacob on January 02, 2002 12:59 PM

Hi

This is possible but not easy. You would have to put code in your macro to save all the changes made. There is an example of the at www.j-walk.com/ss but it probably isnt worth the trouble.

Jacob

Posted by Adam S. on January 02, 2002 3:27 PM

possible work-arounds

1. How about tweaking your macros to save the file before running the rest of your code (assuming you don't have any code that already saves the file)?

The advantage: You can simply close without saving and reopen to the most recent point before you hit the wrong button.

The Disadvantage: This only works if you if you notice you ran the wrong macro immediately after running it. If you don't catch it before running another macro that also saves right off, you'll get stuck with those changes.

2. You could also go with a preventive approach: start the code with a MsgBox that asks if you're running the correct macro ("Did you mean to run the x-macro at this time? Y/N).

Advantage: You'll probably catch yourself before running the wrong macro.

Disadvantage: Msg boxes can get irritating fast.

Hope that helps
Adam S.

Posted by Bariloche on January 02, 2002 3:45 PM

nother possible work around:

Tim,

Is there a unique modification on the spreadsheet that you can check for at the beginning of your macro, so that if that unique item or whatever exists your macro "knows" its already done its thing?

With that said, you could always add some sort of unique thing to the sheet and check for it.

enjoy