Option Explicit
Const LiveWS As String = "Sheet1"
Const AuditWS As String = "Audit"
Private Sub Workbook_Open()
Dim iRow As Integer
Dim iCol As Integer
Dim iLastRow As Long, msgRow As Long
Dim msg As String
If Sheets(AuditWS).Cells(2, 1) = "" Or Sheets(AuditWS).Cells(2, 2) = "" Or Sheets(AuditWS).Cells(2, 2) = "" Then
Sheets(AuditWS).Cells(2, 1) = Environ("USERNAME")
Sheets(AuditWS).Cells(2, 2) = Format(Now(), "dd/mm/yyyy")
Sheets(AuditWS).Cells(2, 3) = Format(Now(), "hh:mm:ss")
End If
'Default msg if no changes are made
msg = "Workbook was opened by " & Sheets(AuditWS).Cells(2, 1) _
& " on " & Sheets(AuditWS).Cells(2, 2) & " at " & Format((Sheets(AuditWS).Cells(2, 3)), "hh:mm:ss") _
& ", they did not save any changes"
'Reserve row for main message to be displayed
msgRow = Sheets(AuditWS).Cells(Rows.Count, 1).End(xlUp).Row
iLastRow = msgRow + 1
'Detect and record if changes were made
For iRow = 1 To 400
For iCol = 1 To 200
If Sheets(AuditWS).Cells(iRow, iCol + 10).Value <> Sheets(LiveWS).Cells(iRow, iCol).Value Then
'change msg if changes are detected
msg = "Workbook was opened by " & Sheets(AuditWS).Cells(2, 1) _
& " on " & Sheets(AuditWS).Cells(2, 2) & " at " & Format((Sheets(AuditWS).Cells(2, 3)), "hh:mm:ss") _
& ", they made following changes:"
Sheets(AuditWS).Cells(iLastRow + 1, 1) = AlphaCol(iCol) & CStr(iRow)
Sheets(AuditWS).Cells(iLastRow + 1, 2) = Sheets(AuditWS).Cells(iRow, iCol + 10).Value
Sheets(AuditWS).Cells(iLastRow + 1, 3) = Sheets(LiveWS).Cells(iRow, iCol).Value
'Update last row after above entries
iLastRow = Sheets(AuditWS).Cells(Rows.Count, 1).End(xlUp).Row
'update the comaprsion records in audit sheet
Sheets(AuditWS).Cells(iRow, iCol + 10) = Sheets(LiveWS).Cells(iRow, iCol).Value
End If
Next iCol
Next iRow
Sheets(AuditWS).Cells(msgRow + 1, 1) = msg
'Record current user's data for next record entry
Sheets(AuditWS).Cells(2, 1) = Environ("USERNAME")
Sheets(AuditWS).Cells(2, 2) = Format(Now(), "dd/mm/yyyy")
Sheets(AuditWS).Cells(2, 3) = Format(Now(), "hh:mm:ss")
ActiveWorkbook.Save
End Sub
Public Function AlphaCol(argColumn As Integer) As String
Dim intPrefix As Integer
Dim strPrefix As String
intPrefix = 0
Do Until argColumn <= 26
intPrefix = intPrefix + 1
argColumn = argColumn - 26
Loop
If intPrefix > 0 Then strPrefix = Chr(intPrefix + 64)
AlphaCol = strPrefix & Chr(argColumn + 64)
End Function