我有这段代码用来调用旧值和新值。但是我现在遇到一个问题,如果用户只是突出显示 a 并点击删除,它将运行并注意到更改,但是由于某种原因它将运行相同请求的更多实例。有没有办法让它只运行一次?谢谢
Public OldValues As New Collection
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Sheets("Pagination").Range("J11") <> "Yes" Then Exit Sub
'Copy old values
Set OldValues = Nothing
Dim c As Range
For Each c In Target
OldValues.Add c.Value, c.Address
Next c
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Sheets("Pagination").Range("J11") <> "Yes" Then Exit Sub
On Local Error Resume Next ' To avoid error if the old value of the cell address you're looking for has not been copied
Dim c As Range
For Each c In Target
Sheets("corrections").Cells(Rows.Count, "A").End(xlUp)(2).Value = Now & " Sheet " & ActiveSheet.Name & " Cell " & Target.Address(0, 0) & " has a new value of " & c.Value & "; old value was " & OldValues(c.Address)
Next c
'Copy old values (in case you made any changes in previous lines of code)
Set OldValues = Nothing
For Each c In Target
OldValues.Add c.Value, c.Address
Next c
End Sub