Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, j As Long, k As Long, arr As Variant
On Error GoTo ErrTrap:
If Target.Areas.Count > 1 Then
Application.EnableEvents = True
MsgBox "sorry, you can change only one contignous range at once, old values restored", vbCritical
Else
If Target.Cells.Count > 1 Then
arr = Target.Value
Else
ReDim arr(1 To 1, 1 To 1)
arr(1, 1) = Target.Value
End If
Application.EnableEvents = False
Application.Undo
With Sheets("AuditLog")
Sheets("AuditLog").Unprotect Password:="..."
k = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
For i = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
If arr(i, j) <> Target.Cells(i, j) Then
Debug.Print Target.Cells(i, j).Address, Target.Cells(i, j), arr(i, j)
.Cells(k, 1) = Application.UserName
.Cells(k, 2).Value = Environ$("computername")
.Cells(k, 3).Value = "changed cell"
.Cells(k, 4).Value = Target.Cells(i, j).Address
.Cells(k, 5).Value = Cells(Target.Cells(i, j).Row, "C").Value ' "To list referance to item code from column C"
.Cells(k, 6).Value = Cells(Target.Cells(i, j).Row, "B").Value ' "To list referance to GRN from column B"
.Cells(k, 7).Value = Cells(Target.Cells(i, j).Row, "D").Value ' "To list referance to LOT from column D"
.Cells(k, 8).Value = Target.Cells(i, j)
.Cells(k, 9).Value = arr(i, j)
.Cells(k, 10).Value = Now()
.Cells(k, 11).Value = ActiveSheet.Name
k = k + 1
End If
Next j, i
End With
Application.Undo ' this will ReDo last UnDo in case if I forget later !!! :-) !!!
Application.EnableEvents = True
Sheets("AuditLog").Protect Password:="...", UserInterfaceOnly:=True
Sheets("AuditLog").EnableAutoFilter = True
Sheets("AuditLog").AllowSorting = True
End If
Exit Sub
ErrTrap:
' keep events enabled reminder
Application.EnableEvents = True
' other errors could be also handled here
End Sub
Bookmarks