19. VBA Tips - Log An Audit Trail
Logging an audit trail of your changes can be useful, and can be done with a small amount of VBA...
Here's our data:
Here's a screen shot of what we're trying to achieve:
Here's the code to do it (put in the worksheet's macro module):
Explanation:
- PreviousValue is a variable that we Dim outside the sub routines so that it's available to both routines
- When you select a different cell, PreviousValue is set to the value of the cell that you have selected
- This is set via the Worksheet_SelectionChange event
- When you change a cell's value, the Worksheet_Change sub routine is run and it compares the new value (Target.value) with PreviousValue... logging if there has been a change.
- The Log sheet contains details of the changes including the name of the person who changed the cell, what cell was changed, and what it went from and to.
Download sheet to practise how to Log An Audit Trail in Excel
Training Video on how to Log An Audit Trail in Excel:
Attachment | Size |
---|---|
log-an-audit-trail.xls | 40 KB |
»
- Nick's blog
- Login or register to post comments
- 180784 reads
Audit trailing
Log an audit trail
I think this might be what you want then:
Change:
If Target.Value <> PreviousValue Then
to:
If Target.Value <> PreviousValue and Target.Value <> 0 Then
code for logging the changes in a workbook
is it possible for you to publish a code that logging the changes in the entire workbook, and not only in one worksheet??
regards,
Ziv
code for logging the changes in a workbook
Hi
All you have to do is to add the code to each worksheet you want to log changes to.
... and if you want to add the worksheet name, change the code to:
Nick
Following code in
Following code in Thisworkbook macro module can log the changes in whole workbook except the one named 'Log' for logging.
Dim PreviousValue
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Log" Then Exit Sub
If Target.Value <> PreviousValue Then
Application.EnableEvents = False
Sheets("log").Cells(65000, 1).End(xlUp).Offset(1, 0).Value = _
Application.UserName & " changed cell " & Sh.Name & "." & Target.Address _
& " from " & PreviousValue & " to " & Target.Value
Application.EnableEvents = True
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
PreviousValue = Target.Value
End Sub
code for logging the changes in a workbook
log changes to file