I came across this post on the Microsoft Answers web site and wanted to share it here with everyone.
Could this suggested VBA code actually work to allow the Undo button to work after VBA macro has run?
Person Asks Question:
I have code that runs a sort of Column A after I hit the Enter key. But, I've noticed that if I want to undo what I just did or beyond that, it doesn't let me. Is there VBA code that can be used to enable the Undo?
Answer or Solution:
Yes, you can use the Application.EnableEvents property to enable undoing actions after running an event in VBA code.
By default, when an event is triggered in Excel (such as by pressing the Enter key), the EnableEvents property is set to False, which prevents undoing actions. However, you can set EnableEvents to True in your VBA code to allow undoing actions.
The VBA code:
In this example, the Worksheet_Change event is triggered whenever a cell in the worksheet is changed. The code checks if the changed cell is in column A and if it is, it sorts column A in ascending order.
To enable undoing actions, the Application.EnableEvents property is set to False at the beginning of the code to prevent an infinite loop (since sorting the column would trigger the Worksheet_Change event again), and then set back to True at the end of the code to allow undoing actions.
My Question:
In my case I have a small table with 6 Columns and I'd like to have the ability to "Undo" mistakes made in Columns E and F
How might this code be modified to work in my case / example attached below:
My current VBA code:
Could this suggested VBA code actually work to allow the Undo button to work after VBA macro has run?
Person Asks Question:
I have code that runs a sort of Column A after I hit the Enter key. But, I've noticed that if I want to undo what I just did or beyond that, it doesn't let me. Is there VBA code that can be used to enable the Undo?
Answer or Solution:
Yes, you can use the Application.EnableEvents property to enable undoing actions after running an event in VBA code.
By default, when an event is triggered in Excel (such as by pressing the Enter key), the EnableEvents property is set to False, which prevents undoing actions. However, you can set EnableEvents to True in your VBA code to allow undoing actions.
The VBA code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False ' Disable events to prevent infinite loop
If Target.Column = 1 Then ' Sort column A only
Range("A1"). Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess
End If
Application.EnableEvents = True ' Enable events to allow undoing actions
End Sub
In this example, the Worksheet_Change event is triggered whenever a cell in the worksheet is changed. The code checks if the changed cell is in column A and if it is, it sorts column A in ascending order.
To enable undoing actions, the Application.EnableEvents property is set to False at the beginning of the code to prevent an infinite loop (since sorting the column would trigger the Worksheet_Change event again), and then set back to True at the end of the code to allow undoing actions.
My Question:
In my case I have a small table with 6 Columns and I'd like to have the ability to "Undo" mistakes made in Columns E and F
How might this code be modified to work in my case / example attached below:
My current VBA code:
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
'Clear the color of all cells
Sh.Cells.Interior.ColorIndex = 0
With Target
'Highlight row and column of the selected cell
.EntireRow.Interior.ColorIndex = 24
.EntireColumn.Interior.ColorIndex = 24
End With
Application.ScreenUpdating = True
End Sub
Attachments
Last edited by a moderator: