2

我需要一个函数来检查 vba 中特定范围的单元格是否已更改:

Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("A9:J100")) Is Nothing Then
      MsgBox Target.Address
   End If
End Sub

此代码有效,问题是:它们更改为相同的值(引发互联网更新)。如何检查更新的单元格值是否真的更改为新的?(不一一检查)

提前致谢

4

1 回答 1

4

在这个小例子中,我们维护数据块的内存,因此可以测试是否有任何值实际发生了变化。请注意,内存已在 sub 上方变暗以使其成为静态:

Dim MemoryOfThingsPast() As Variant
Dim init As Long
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Interesting As Range
    Dim I As Long, J As Long, K As Long
    Set Interesting = Range("A9:J100")
    If init = 0 Then
        MemoryOfThingsPast = Interesting
        init = 1
        Exit Sub
    End If
    If Intersect(Target, Interesting) Is Nothing Then Exit Sub
    K = 1
    For I = LBound(MemoryOfThingsPast, 1) To UBound(MemoryOfThingsPast, 1)
        For J = LBound(MemoryOfThingsPast, 2) To UBound(MemoryOfThingsPast, 2)
            v1 = Interesting(K)
            v2 = MemoryOfThingsPast(I, J)
            If v1 <> v2 Then
                MsgBox "Cell " & Interesting(K).Address & " has really changed"
            End If
            K = K + 1
        Next J
    Next I
    MemoryOfThingsPast = Interesting
End Sub
于 2013-09-30T11:49:44.237 回答