VBA audit trail - old value not archive

Flashback

New Member
Joined
Oct 24, 2018
Messages
6
Hello,

im using this code for audit trail, but he is not archive old value. For example when I changed 5 to 10. In log I will have new value only.

Could you help me please? Im total newb in VBA.

Thank you!

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Rw As Long
Dim strAddress As String
Dim strUserName As String
Dim dtmTime As Date
Dim val As Variant


If Intersect(Target, Range("A:M")) Is Nothing Then Exit Sub


dtmTime = Now()
val = Target.Value
strAddress = Target.Address
strUserName = Environ("UserName")
Rw = Sheets("Log Sheet").Range("A" & Rows.Count).End(xlUp).Row + 1
With Sheets("Log Sheet")

.Cells(Rw, 1) = strUserName
.Cells(Rw, 2) = strAddress
.Cells(Rw, 3) = val
.Cells(Rw, 4) = dtmTime

End With
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Like so:
Code:
Option Explicit

Private mvOldValues As Variant

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Rw As Long
    Dim strAddress As String
    Dim strUserName As String
    Dim dtmTime As Date
    Dim val As Variant
    Dim lRow As Long
    Dim lCol As Long
    Dim vNewVal As Variant
    If Intersect(Target, Range("A:M")) Is Nothing Then Exit Sub
    'More than one cell may be affected!!!
    vNewVal = Target.Value
    If Target.Cells.Count > 1 Then
        Rw = Sheets("Log Sheet").Range("A" & Rows.Count).End(xlUp).Row
        For lRow = LBound(mvOldValues, 1) To UBound(mvOldValues, 1)
            For lCol = LBound(mvOldValues, 2) To UBound(mvOldValues, 2)
                If vNewVal(lRow, lCol) <> mvOldValues(lRow, lCol) Then
                    Rw = Rw + 1
                    With Sheets("Log Sheet")
                        .Cells(Rw, 1) = strUserName
                        .Cells(Rw, 2) = strAddress
                        .Cells(Rw, 3) = mvOldValues(lRow, lCol)
                        .Cells(Rw, 4) = vNewVal(lRow, lCol)
                        .Cells(Rw, 5) = dtmTime
                    End With
                End If
            Next
        Next
    Else
        dtmTime = Now()
        val = Target.Value
        strAddress = Target.Address
        strUserName = Environ("UserName")
        Rw = Sheets("Log Sheet").Range("A" & Rows.Count).End(xlUp).Row + 1
        With Sheets("Log Sheet")
            .Cells(Rw, 1) = strUserName
            .Cells(Rw, 2) = strAddress
            .Cells(Rw, 3) = mvOldValues
            .Cells(Rw, 4) = vNewVal
            .Cells(Rw, 5) = dtmTime
        End With
    End If

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    mvOldValues = Target.Value
End Sub
 
Upvote 0
Thank you!

And is possible exclude changes of blank cells?

And this not code not work, when value is copied.. is it possible?

Thank you very much!
 
Upvote 0
Change this line:
Code:
If vNewVal(lRow, lCol) <> mvOldValues(lRow, lCol) Then
to:
Code:
If vNewVal(lRow, lCol) <> mvOldValues(lRow, lCol) And Len(mvOldValues(lRow, lCol)) > 0 Then
Not sure what you mean code not work when value is copied, can you describe the EXACT steps?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
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