在这个小例子中,我们维护数据块的内存,因此可以测试是否有任何值实际发生了变化。请注意,内存已在 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