在更改之前检测单元格中的值

Detecting what value was in a cell prior to a change

有没有办法检测单元格在更改之前用来包含什么。我正在尝试根据单元格中的先前值执行操作。我知道有 Worksheet_Change 但它使用的 Target 是新值。

我们必须记住里面有什么。假设单元格 B9 很有趣。在 标准模块 中包含单行:

Public OldValue As Variant

并在工作表代码区:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B9")) Is Nothing Then Exit Sub
    v = Target.Value
    MsgBox v & vbCrLf & OldValue
    OldValue = v
End Sub

您可以撤消更改,将撤消的值填充到变量中,然后像这样重做更改:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim OldValue As Variant, NewValue As Variant
NewValue = Target.Value
Application.EnableEvents = False
Application.Undo
OldValue = Target.Value
Target.Value = NewValue
Application.EnableEvents = True
MsgBox OldValue
End Sub