Detecting what value was in a cell prior to a change

ぐ巨炮叔叔 提交于 2019-12-11 23:16:48

问题


Is there a way to detect what a cell use to contain before it was changed. I'm trying to perform actions based on what the previous value was in a cell. I know there's Worksheet_Change but the Target it uses is the new value.


回答1:


You can undo the change, populate the undone value to a variable then redo the change like so:

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



回答2:


We must remember what was in there. Say cell B9 was of interest. In a Standard Module include the single line:

Public OldValue As Variant

and in the worksheet code area:

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


来源:https://stackoverflow.com/questions/28841415/detecting-what-value-was-in-a-cell-prior-to-a-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!