行者

慢慢走,不要停
  博客园  :: 新随笔  :: 管理

ExcelVba 单元格历史记录(记录单元格变更历史)

Posted on 2023-12-14 22:52  angury  阅读(157)  评论(0编辑  收藏  举报
Public TrackChanges As Boolean

Sub ToggleCellHistory()
    TrackChanges = Not TrackChanges
    If TrackChanges Then
        MsgBox "单元格历史记录功能已开启。"
    Else
        MsgBox "单元格历史记录功能已关闭。"
    End If
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If TrackChanges Then
        Dim cell As Range
        For Each cell In Target
            If cell.Comment Is Nothing Then
                cell.AddComment
            End If
            cell.Comment.Text Text:=Format(Now, "yyyy-mm-dd hh:mm:ss") & ": " & cell.Value & Chr(10) & cell.Comment.Text
        Next cell
    End If
End Sub