VBA audit trail - modifications

Flashback

New Member
Joined
Oct 24, 2018
Messages
6
Hello,

pleas, how I could exclude changes of blank value? I no need log, when emty cell is changed.

Thank you!

Code:
Dim PreviousValues As Variant

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Store current values of all cells
    Application.EnableEvents = True
    PreviousValues = Me.UsedRange.Value
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim LR     As Long
    Dim c      As Range
    
    On Error Resume Next
    
    Application.EnableEvents = False
       If Target(1).Value <> PreviousValues(Target(1).row, Target(1).Column) Then
            With Sheets("LogSheet")
            For Each c In Target
                LR = .Cells(Rows.Count, "A").End(xlUp).row + 1
                .Cells(LR, "A").Value = ActiveSheet.Name & "!" & c.Address
                .Cells(LR, "B").Value = Now
                .Cells(LR, "C").Value = Environ("UserName")
                .Cells(LR, "D").Value = PreviousValues(c.row, c.Column)
                .Cells(LR, "E").Value = c.Value
            Next c
        End With
    End If
    Application.EnableEvents = True
    
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Make these changes to your Worksheet_Change procedure:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim LR     As Long
    Dim c      As Range
[COLOR=#ff0000]    Dim pval   As Variant[/COLOR]
    
    On Error Resume Next
    
    Application.EnableEvents = False
       If Target(1).Value <> PreviousValues(Target(1).Row, Target(1).Column) Then
            With Sheets("LogSheet")
            For Each c In Target
[COLOR=#ff0000]                pval = PreviousValues(c.Row, c.Column)
                If pval <> "" Then[/COLOR]
                    LR = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                    .Cells(LR, "A").Value = ActiveSheet.Name & "!" & c.Address
                    .Cells(LR, "B").Value = Now
                    .Cells(LR, "C").Value = Environ("UserName")
                    [COLOR=#ff0000].Cells(LR, "D").Value = pval[/COLOR]
                    .Cells(LR, "E").Value = c.Value
                End If
            Next c
        End With
    End If
    Application.EnableEvents = True
    
End Sub
 
Upvote 0
Note the code I posted JUST replaces the Worksheet_Change procedure!
You still need the global variable declaration and the Worksheet_Selection change procedure that you had in your original post (don't remove or overwrite those)!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top