Audit Trail

Spikenaylor

Board Regular
Joined
Apr 14, 2013
Messages
116
Office Version
  1. 365
Platform
  1. Windows
My end users enter data in the sheet for a batch, then they press a button to run code, check for certain errors, then lock the cells. Audit trail is populated with Date, Time, Excel User ID, Batch Number, worksheet tab.
Code:
Sub AuditTrail(batchnumber As String)

        Sheets("Audit_Trail").Cells(65536, 1).End(xlUp).Offset(1, 0).Value = _
            " Date - " & Date & "          " & " Time - " & Time & "          " & " User - " & Application.UserName & "          Batch number Locked - " & batchnumber & "          " & " Sheet - " & ActiveSheet.Name


End Sub

If there are errors in the data, ie someone has entered 35000 instead of 30000, then a Team leader unlocks the Batch via a unlock Batch button, changes the data then locks the batch.

The unlock Batch button runs code to capture audit trail details Date, time, Excel User ID, Batch Number unlocked, Reason for Unlocking, worksheet tab.

Code:
Sub AuditTrail(batchnumber As String)
Recheck:
batchunlockmessage = InputBox("Please enter the reason you are unlocking the Batch", "Unlock Batch Reason")
If batchunlockmessage = vbNullString Then
MsgBox ("You must enter a reason for unlocking the batch!")
GoTo Recheck
End If


        Sheets("Audit_Trail").Cells(65536, 1).End(xlUp).Offset(1, 0).Value = _
            " Date - " & Date & "          " & " Time - " & Time & "          " & " User - " & Application.UserName _
            & "          Batch number Unlocked - " & batchnumber & "         Reason for Unlocking - " & batchunlockmessage _
            & "          " & " Sheet - " & ActiveSheet.Name

End Sub

I now need to capture the data actually changed during the unlock and lock actions.

I have this code for Audit trail, but not sure how to implement alongside only during the unlock and lock actions.
Code:
Dim PreviousValue
 
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Value <> PreviousValue Then
        Sheets("Audit_Trail").Cells(65000, 1).End(xlUp).Offset(1, 0).Value = _
            Application.UserName & " changed cell " & Target.Address _
            & " from " & PreviousValue & " to " & Target.Value & " In Sheet " & ActiveSheet.Name
    End If
End Sub

Any guidance or help appreciated.
regards
Spikenaylor
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
One suggestion would be to use global variables to store your custom states (ie locked,unlocked etc). Use the lock and unlock button codes to set these states and then look for them in your Worksheet_Change code before executing anything.
 
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