|
Application.Undo会批量进行操作,所以用两个临时数组记录内容,然后进行比较并记录。
- Private Sub Worksheet_Change(ByVal Target As Range)
- ' Const xRg As String = "A1:ZZ1000"
- Dim strOld As String
- Dim strNew As String
- Dim strCmt As String
- Dim xLen As Long
- Dim i, j
- Dim arr, brr
-
- Application.EnableEvents = False
- arr = UsedRange
- Application.Undo
- brr = UsedRange
-
- ' Stop
- If Target.Count = 1 Then
- With Target
- i = Target.Row
- j = Target.Column
- If Target.Value = arr(i, j) Then Exit Sub
- strNew = arr(i, j)
- ' Application.EnableEvents = False
- ' Application.Undo
- strOld = brr(i, j)
- .Value = strNew
- ' Application.EnableEvents = True
- strCmt = "Edit: " & Format$(Now, "dd Mmm YYYY hh:nn:ss") & " by " & _
- Application.UserName & Chr(10) & "Previous Text :- " & strOld
- If Target.Comment Is Nothing Then
- .AddComment
- Else
- xLen = Len(.Comment.Shape.TextFrame.Characters.Text)
- End If
- With .Comment.Shape.TextFrame
- .AutoSize = True
- .Characters(Start:=xLen + 1).Insert IIf(xLen, vbLf, "") & strCmt
- End With
- End With
- ElseIf Target.Count > 1 Then
- For Each Rng In Target
- With Rng
- i = Rng.Row
- j = Rng.Column
- If Rng.Value = arr(i, j) Then Exit Sub
- strNew = arr(i, j)
- ' Application.EnableEvents = False
- ' Application.Undo
- strOld = brr(i, j)
- .Value = strNew
- ' Application.EnableEvents = True
- strCmt = "Edit: " & Format$(Now, "dd Mmm YYYY hh:nn:ss") & " by " & _
- Application.UserName & Chr(10) & "Previous Text :- " & strOld
- If Rng.Comment Is Nothing Then
- .AddComment
- Else
- xLen = Len(.Comment.Shape.TextFrame.Characters.Text)
- End If
- With .Comment.Shape.TextFrame
- .AutoSize = True
- .Characters(Start:=xLen + 1).Insert IIf(xLen, vbLf, "") & strCmt
- End With
- End With
-
- Next
- End If
- Application.EnableEvents = True
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|