Audit Trail ... help.

wisewood

Board Regular
Joined
Nov 7, 2002
Messages
193
I have an important spreadsheet, which several people have access to and make changes to it.

I have come up with an idea, and would like to keep an AuditTrail so that if something goes horribly wrong with it, we have a record to look back and find out who did it, and when.

So far, i have a script which runs when the selection is changed, and takes a copy of the current row and pastes it onto a sheet called "AuditTrail".

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Limit = Sheets("AuditTrail").Cells(Rows.Count, 1).End(xlUp).Row + 1
Rows(ActiveCell.Row).Copy Destination:=Sheets("AuditTrail").Rows(Limit)
End Sub

The problem with this is that if the user scrolls up and down or left and right, you end up with lots of un-necessary rows on the audit trail... i want it to only copy the row if the content of any of the cells is changed.

Also, i would like to paste the information onto AuditTrail, but not at column A, i would like it pasted at Column C, with Column A and B being the date & time, and the username of the person who made the change.

Can anyone help me out on this please?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Firstly, try using a Worksheet_Change or Worksheet_Calculate event - then the code will only run when a cell is changed or the sheet calculates.

To enter the additional info on the Audit Trail sheet, use the following:

Code:
With Sheets("AuditTrail")
.Cells(Limit,1) = Now
.Cells(Limit,2) = Environ("Username")
End With
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Limit1 As Long, Limit2 As Integer
Limit1 = Sheets("AuditTrail").Cells(Rows.Count, 1).End(xlUp).Row + 1
Limit2 = Cells(Target.Row, Columns.Count).End(xlToLeft).Column
Range(Cells(Target.Row, 1), Cells(Target.Row, Limit2)).Copy Destination:=Sheets("AuditTrail").Range("C" & Limit1)
Sheets("AuditTrail").Range("A" & Limit1).Value = Environ("UserName")
Sheets("AuditTrail").Range("B" & Limit1).Value = Now
End Sub
 
Upvote 0
Perfect ... now ... how do i get the row & column (eg B132) number of the cell that has been changed also - i just realised that would make life SO much easier when trying to find where a change was made.
 
Upvote 0
This adds the address in column C

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Limit1 As Long, Limit2 As Integer
Limit1 = Sheets("AuditTrail").Cells(Rows.Count, 1).End(xlUp).Row + 1
Limit2 = Cells(Target.Row, Columns.Count).End(xlToLeft).Column
Range(Cells(Target.Row, 1), Cells(Target.Row, Limit2)).Copy Destination:=Sheets("AuditTrail").Range("D" & Limit1)
With Sheets("AuditTrail")
    .Range("A" & Limit1).Value = Environ("UserName")
    .Range("B" & Limit1).Value = Now
    .Range("C" & Limit1).Value = Target.Address(False, False)
End With
End Sub
 
Upvote 0
Hi,

I am not sure if I need to start a thread but I am using the code pasted above a little differently. In the code for Sheet1 I pasted:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Limit1 As Long
Limit1 = Sheets("DomAuditTrail").Cells(Rows.Count, 1).End(xlUp).Row + 1
With Sheets("DomAuditTrail")
.Range("A" & Limit1).Value = Environ("UserName")
.Range("B" & Limit1).Value = Now
End With
End Sub

However what I would like to do is in cell A1 in the Sheet1 tab i want it to show "Last Updated on "m/dd/yyyy;h:mm" by "user". Is there anyway to add this to the above code? Lastly, is it possible for the DomAuditTrail to track only what happens below Row 3?

Thanks for your help!
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Limit1 As Long
If Target.Row < 3 Then Exit Sub
Limit1 = Sheets("DomAuditTrail").Cells(Rows.Count, 1).End(xlUp).Row + 1
With Sheets("DomAuditTrail")
    .Range("A" & Limit1).Value = Environ("UserName")
    .Range("B" & Limit1).Value = Now
End With
Range("A1").Value = "Last updated on " & Format(Now, "m/dd/yyyy h:mm") & " by " & Environ("username")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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