撤消功能以恢复重置内容宏

Undo function to revert the reset contents macro

我在 Sheet 上有一个 Active X 控制按钮,它将 "reset contents" of Sheet。

我还想添加另一个名为 "Undo Button" 的按钮,该按钮应该恢复使用 "Reset Contents" 清除的内容。这可能吗?

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

请建议

假设在您的相关 ("DropSheet"?) 工作表中有:

  • 以 "CommandButton21"

  • 命名的 ActiveX 按钮
  • 以 "UndoBtn"

  • 命名的 ActiveX 按钮

将此代码放在同一个工作表代码窗格中:

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