# Microsoft Office Application Help - Excel Help forum > Excel Programming / VBA / Macros >  > [SOLVED] Undoing a macro

## MissyLovesExcel

Once a macro is run, can you "undo" it? Or, is there a second macro that will
reverse any previous macro just run?

----------


## Niek Otten

No, You'll have to take care of that yourself

--
Kind regards,

Niek Otten

"MissyLovesExcel" <MissyLovesExcel@discussions.microsoft.com> wrote in
message news:41AE8C27-C02F-456E-8CF9-47BC5893BDC1@microsoft.com...
> Once a macro is run, can you "undo" it? Or, is there a second macro that
> will
> reverse any previous macro just run?

----------


## Norman Jones

Hi Missy,

See John Walkenbach's  Undo notes at:

http://www.j-walk.com/ss/excel/tips/tip23.htm

---
Regards,
Norman



"MissyLovesExcel" <MissyLovesExcel@discussions.microsoft.com> wrote in
message news:41AE8C27-C02F-456E-8CF9-47BC5893BDC1@microsoft.com...
> Once a macro is run, can you "undo" it? Or, is there a second macro that
> will
> reverse any previous macro just run?

----------


## Jim Thomlinson

Nope. There is no Back button. Run is run and deleted is gone. You could
write something to reverse some actions but some actions are permanent. Be
very careful what you write and prompt for any action that can not be undone.
The undo button is also unavailable after the macro is run. You can use
application.undo while a macro is running to undo the last action the last
action performed.
--
HTH...

Jim Thomlinson


"MissyLovesExcel" wrote:

> Once a macro is run, can you "undo" it? Or, is there a second macro that will
> reverse any previous macro just run?

----------


## GregR

Missy, you can't undo a macro, but you can revert the file to the state
it was before the macro was run with this code:
Sub RevertFile()

wkname = ActiveWorkbook.path & "\" & ActiveWorkbook.Name
ActiveWorkbook.Close Savechanges:=False

Workbooks.Open FileName:=wkname

End Sub

HTH

Greg

----------


## Ed

That would seem to fall apart if the macro contained a Save command.  And
really it's no different than saving the file just before you run your
macro, then closing without saving changes if you don't like the results.

"GregR" <gregrivet@gmail.com> wrote in message
news:1133888422.012728.58540@g14g2000cwa.googlegroups.com...
> Missy, you can't undo a macro, but you can revert the file to the state
> it was before the macro was run with this code:
> Sub RevertFile()
>
> wkname = ActiveWorkbook.path & "\" & ActiveWorkbook.Name
> ActiveWorkbook.Close Savechanges:=False
>
> Workbooks.Open FileName:=wkname
>
> End Sub
>
> HTH
>
> Greg
>

----------


## GregR

Ed, you are absolutely right, but if it doesn't contain a save command,
it is an alternative to recover the file.

Greg

----------


## Harald Staff

Hi Greg

You are assuming that the macro is simply doing things to 'the' file. A
single macro can delete gigs of files from a hard drive, mess up the
registry, refurnish the desktop, corrupt a few databases, email lots of
nigerian spam, you name it. The undo buffer would have to be infinite, so no
one could afford to create it.

Best wishes Harald

"GregR" <gregrivet@gmail.com> skrev i melding
news:1133897222.828025.290760@g49g2000cwa.googlegroups.com...
> Ed, you are absolutely right, but if it doesn't contain a save command,
> it is an alternative to recover the file.
>
> Greg
>

----------


## GregR

Harald, I absolutely agree, and if any of the concerns expressed by
Harald or Ed pertain to your situation my solution will not work. I am
just trying to express a solution to a simple macro that only acts on
the active file and does not contain a save command. HTH

Greg

----------


## ricohflex

> Missy, you can't undo a macro, but you can revert the file to the state
> it was before the macro was run with this code:
> Sub RevertFile()
> 
> wkname = ActiveWorkbook.path & "\" & ActiveWorkbook.Name
> ActiveWorkbook.Close Savechanges:=False
> 
> Workbooks.Open FileName:=wkname
> 
> ...



I registered for this forum - just to be able to say a big Thank You to GregR. 
Although others have critiqued the VBA code by saying that it only works if you had not saved the Excel file after you executed the macro (that you now want to undo). I thought that was obvious to seasoned PC users. 
It does not detract from the usefulness of GregR's contribution. 
I used his VBA code, saved it, assigned it to a custom button on the Excel toolbar and pasted an icon picture of a reversing rounded arrow.
It works very well. 
I was looking for a way to Undo a macro used to sort an Excel range. 
This is it.

----------

