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

时间:2016-10-27 16:26:18

标签: excel vba excel-vba undo

我在Sheet上有一个Active X控制按钮,它将重置内容" of Sheet。

我还想添加另一个名为"撤消按钮"应该使用"重置内容"来恢复清除的内容。这可能吗?

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

请建议

1 个答案:

答案 0 :(得分:1)

假设在您的相关(“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