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
- 184605 reads
Add date to audit trail
This macro is SUPERB!
Is it possible to protect the log worksheet so that users can't modify it? Thanks!
log changes
yes.. just Record a Macro of protecting and unprotecting the sheet with a password.
unprotect at the beginning, and protect at the end.
Changing multiple cells simultaneously
Great post, by the way.
Is there any way that the code can be changed to handle copying and pasting more than one cell at a time?
Nick
Changing multiple cells simultaneously
NOTE the result of copying col 1 into col 2:
- you get the same cell address
Changing multiple cells
Hi Nick,
i input this code and tested it and was working great. I now have entered the spreadsheet again and it no longer records the log. It doesn't give me any errors or anything. I've checked the code and it's exactly the same. Do you know why this would be happening? No one has opened it since. Not sure why it would just stop working as the code is still there.
Thanks,
Did you enable macros when
Did you enable macros when opening ?
Also, are events enabled.. maybe another bit of VBA turns off events.
- try opening in a new Excel session making sure macros are enabled.
Audit trail for Merged Cells
Hi Nick,
Thanks for the codes. They are very helpful.
In adidition to the code where changes to more than one cell at a time is recorded, is it possible to have a code to audit trail 5 cells, which are merged.
merged cells
merged cells count as 1, no ?
You can pick up all the cells in a merged range, use Selection.MergeArea
Using selection.mergearea
what is the proper syntax for this? how can we use this? I tried using this by changing this original code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
PreviousValue = Target.Value
End Sub
to
Private Sub Worksheet_SelectionMergeArea(ByVal Target As Range)
PreviousValue = Target.Value
End Sub
-- It seemed to work but then it appears that the logs created are always stuck at the PreviousValue returning a 'blank' value.