VBA Code to create an audit trail in Excel 2010

Confused_UK

Board Regular
Joined
Dec 19, 2015
Messages
56
Hi all

I am looking for some VBA code to record any changes to a workbook in one worksheet called "ChangesLog".

Ideally I would like it to show the name of the individual making the change, the date and timestamp of the change, which cell in which worksheet has been changed, and the old and new values, but in a table.

I have found the code below which works very well, but would like it broken into individual cells. Does anyone have any ideas?

Code:
[COLOR=#000000][FONT=Verdana]Dim PreviousValue[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana]Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value <> PreviousValue Then
Sheets("log").Cells(65000, 1).End(xlUp).Offset(1, 0).Value = _
Application.UserName & " changed cell " & Target.Address _
& " from " & PreviousValue & " to " & Target.Value & " at: " & Time & " on: " & Date
End If
End Sub[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
PreviousValue = Target.Value
End Sub[/FONT][/COLOR]

Thank you in advance
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
WHat do you mean "individual Cells"? I ran the code above and it works great, I changed something in E14 and it came back as $E$14:$E$14... or do you just want E14?
 
Upvote 0
On the Audit Log, column A would be the UserName, column B the Target Address, etc, rather than one long string
 
Upvote 0
Ahhh, gottcha, here try this:

Code:
Dim PreviousValuePrivate Sub Worksheet_Change(ByVal Target As Range)

Dim i As Long
Dim ws As Worksheet

Set ws = Sheets("log")

i = ws.Range("A" & Rows.Count).End(xlUp).Row + 1

If Target.Value <> PreviousValue Then
    With ws
        .Range("A" & i).Value = Application.UserName
        .Range("B" & i).Value = Target.Address
        .Range("C" & i).Value = PreviousValue
        .Range("D" & i).Value = Target.Value
        .Range("E" & i).Value = Format(Now(), "dd/mm/yyyy, hh:mm:ss")
    End With
End If

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   PreviousValue = Target.Value
End Sub
 
Upvote 0
That's fantastic thank you. If Target.Address is the cell, what would be the reference for the worksheet?
 
Upvote 0
One way is to add this infront of Target.Address

Code:
ws.Name & " | " &
 
Upvote 0
ws.Name gives the Audit worksheet name, in this case "log", rather than the worksheet of the changed cell.
 
Upvote 0
Oh... yea sorry forgot I had the ws linked to log... try this

Code:
ActiveSheet.Name

Instead of

Code:
ws.Name
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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