OnUndo Event
Syntax: Application.OnUndo (Text, Procedure)
This method has two arguments:
Text
Specify text to appear on the Undo menuProcedure
Procedure to execute when the user chooses Undo
The following example causes the Undo menu item to display Undo WeekDays Macro
. If the user chooses Undo WeekDays Macro
, the UndoWeekDays
procedure is executed:
Example: Using Application.OnUndo
to perform Undo operation for a subroutine
Option Explicit Dim cellAddress As Range Sub WeekDays() Set cellAddress = ActiveCell ActiveCell.Value = "Sunday" Selection.AutoFill Destination:=ActiveCell.Range("A1:A7"), Type:=xlFillDefault Application.OnUndo Text:="Undo WeekDays Macro", Procedure:="UndoWeekDays" End Sub Private Sub UndoWeekDays() MsgBox "Undo WeekDays Macro" cellAddress.Select ActiveCell.Range("A1:A7").Clear End Sub
The WeekDays
macro writes the weekdays and stores the ActiveCell
reference in cellAddress
variable which we use later for undo operation. The UndoWeekDays
subroutine runs when the user perform Undo action.