1

我有这段代码用来调用旧值和新值。但是我现在遇到一个问题,如果用户只是突出显示 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
4

1 回答 1

1

添加Application.EnableEvents = FalseWorksheet_Change事件的开头,然后返回到事件Application.EnableEvents = True的末尾。

编辑:我自己无法重新创建您的问题,并且由于宏没有在同一张表上进行更改,因此上述解决方案不应该有所作为。您在“更正”表上是否还有其他Worksheet_Change事件需要禁用此类事件?


Application.EnableEvents还应添加到可能影响工作表中具有worksheet_change事件的任何单元格的其他功能。

例如,即使单元格 A1 的值发生更改,也会调用 worksheet_change 事件。

于 2013-10-09T12:18:30.430 回答