Building Undo Into an Excel VBA Macro

Fred Nurke picture Fred Nurke · Dec 4, 2008 · Viewed 38.2k times · Source

Excel macros do not seem to allow the use of "undo" after running them. Is there any way to bake undo functionality into a VBA macro in Excel?

Answer

e.James picture e.James · Dec 4, 2008

Excel VBA has the Application.OnUndo function to handle this:

Public Sub DoSomething

    ... do stuff here

    Application.OnUndo "Undo something", "UnDoSomething"
End Sub

Public Sub UnDoSomething

    ... reverse the action here

End Sub