Search code examples
excelexcel-2007vba

How to programmatically code an 'undo' function in Excel-Vba?


Is there anyway to code an undo function onto a CommandButton similar to the Excel's very own undo function?

Or a function which is capable of calling Ctrl-Z shortcut key.


Solution

  • Add the command button to the worksheet and assign the following macro to it:

    Sub UndoLastAction()
    
        With Application
            .EnableEvents = False
            .Undo
            .EnableEvents = True
        End With
    
    End Sub
    

    It can only undo the last action taken by the user and cannot undo VBA commands.

    EDIT: If you need further undo capabilities see:

    Undo With Excel VBA - JKP