create user audit trail in microsoft excel 2010

2019-09-10 08:55发布

问题:

I need to create a basic user audit trail in Excel 2010 tracking changes to certain cells by different users not signing into a PC (shared PC)

回答1:

The following macro monitors changes to cells A2 thru A20

If a user changes any of these cells, the username and date are recorded in the cell's comment

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Intersect(Target, Range("A2:A20")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    Dim s As String
    s = Now & vbCrLf & Environ("UserName")
        With Target
            .ClearComments
            .AddComment s
        End With
    Application.EnableEvents = True
End Sub