Audit Log VBA Code Microsoft 365

Deosculate

New Member
Joined
Mar 10, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi everybody,

I've tried a bunch of different codes I've found online for creating an audit log that tracks any changes within a workbook but either they do not work, or they create an endless loop that eventually crashes Excel. Can anyone help me with some code that will track any changes made within the entire workbook, specifically the user name, date, time and the changes made and add these to a worksheet called Log?

Thanks in advance!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Here's what I use for audit. I have a separate sheet that I hide that contains the audit log. There is one cell designated as the 'LastRowInd' that updates based on formula as shown below.

Business Analyst Workbook - Contact Center.xlsm
ABCDEFGH
1Defect Change Audit LogLastRow705
2
3
4
5
6
7Date ChangedChanged ByIssue #Issue Description Related Test CaseSeverityStateSubmitted by
Defect Audit
Cell Formulas
RangeFormula
H1H1=SUMPRODUCT(MAX((A7:A999920<>"")*ROW(A7:A999920)))


The script in each sheet you want audited would look something like the following:
VBA Code:
Private Sub worksheet_change(ByVal target As Range)
If Not Intersect(target, Sheet11.Range("B10:J1000")) Is Nothing Then

   Sheet12.Range("A" & Sheet12.Range("LastRowInd").Value + 1) = Now
   Sheet12.Range("B" & Sheet12.Range("LastRowInd").Value) = Environ("UserName")
   Sheet12.Range("C" & Sheet12.Range("LastRowInd").Value & ":O" & Sheet12.Range("LastRowInd").Value).Value = Sheet11.Range("A" & target.Row & ":M" & target.Row).Value

End If

End Sub
This will add the change date, user ID of the person who made the change, and the corresponding record details.
 
Upvote 0
If you prefer not to use a cell to indicate the last row, you could do something like this:
VBA Code:
dim lastrow as long
If Not Intersect(target, Sheet11.Range("B10:J1000")) Is Nothing Then
lastrow = Sheet12.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1


   Sheet12.Range("A" & lastrow).Value = Now
   Sheet12.Range("B" & lastrow).Value = Environ("UserName")
   Sheet12.Range("C" & lastrow & ":O" & lastrow).Value = Sheet11.Range("A" & target.Row & ":M" & target.Row).Value

End If
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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