我已经创建了这个宏查找件并且适合我的程序:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("N19")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Dim vNew As Integer
Dim vOld As Integer
vNew = Range("N19").Value
Application.EnableEvents = False
Application.Undo
vOld = Range("N19").Value
Range("N19").Value = vNew
Range("D159").Value = vOld
Application.EnableEvents = True
End If
End Sub
我需要在D159中保存N19的旧值。
你知道它为什么不起作用吗?
由于
答案 0 :(得分:0)
您必须将值的分配位置更改为vOld
。在您的代码中,它在N19
之后被分配到Application.Undo
,因此它被分配给任何内容。我试过这个,它有效:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("N19")
If Not Application.Intersect(KeyCells, Target) Is Nothing Then
Dim vNew As Long
Dim vOld As Long
vNew = Range("N19")
vOld = Range("N19")
Application.EnableEvents = False
Application.Undo
vOld = Range("N19")
Range("N19") = vNew
Range("D159") = vOld
Application.EnableEvents = True
End If
End Sub