sady
sady

Reputation: 301

Undo function to revert the reset contents macro

I have a Active X control button on Sheet that will "reset contents" of Sheet.

I also, would like to add an other button called "Undo Button" that should revert the contents cleared using "Reset Contents". Is this possible?

Private Sub CommandButton21_Click()
Worksheets("DropSheet").Range("E7:E15").ClearContents
End Sub

Please Suggest

Upvotes: 0

Views: 3660

Answers (1)

user3598756
user3598756

Reputation: 29421

Assuming that in your relevant ("DropSheet"?) worksheet there are:

  • an ActiveX button named after "CommandButton21"

  • an ActiveX button named after "UndoBtn"

place this code in that very same worksheet code pane:

Option Explicit

Dim lastValues As Variant '<-- worksheet scoped variable where to store "last" values in before CommandButton21 button clears them

Private Sub CommandButton21_Click()
    With Range("E7:E15") '<--| reference your relevant range
        lastValues = .Value '<--| first, store its content in the worksheet scoped array variable
        .ClearContents '<--| then, clear its content
    End With
End Sub

Private Sub UndoBtn_Click()
    Range("E7:E15").Value = lastValues '<--| write'em back!
End Sub

Upvotes: 1

Related Questions