If OldVal <> NewVal Then
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim LR As Long, NewVal As Variant, OldVal As Variant
If Target.Count > 1 Then Exit Sub
If Sh.Name = "Log" Then Exit Sub
If Not Intersect(Target, Range("Data")) Is Nothing Then Exit Sub
Application.EnableEvents = False
NewVal = Target.Value
Application.Undo
OldVal = Target.Value
Target.Value = NewVal
If OldVal <> NewVal Then
With Sheets("Log")
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A" & LR + 1).Value = VBA.Environ("username") 'user
.Range("B" & LR + 1).Value = Now 'date and time
.Range("C" & LR + 1).Value = Sh.Name 'sheet
.Range("D" & LR + 1).Value = Target.Address(False, False) 'cell
.Range("E" & LR + 1).Value = OldVal 'previous value
.Range("F" & LR + 1).Value = Target.Value 'new value
End With
End If
Application.EnableEvents = True
End Sub
If OldVal <> NewVal Then
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim LR As Long, NewVal As Variant, OldVal As Variant
Dim cell As Range
If Target.Count > 1 Then Exit Sub
If Sh.Name = "Log" Then Exit Sub
Application.EnableEvents = False
NewVal = Target.Value
Set cell = ActiveCell
On Error Resume Next
Application.Undo
OldVal = Target.Value
Target.Value = NewVal
If OldVal <> NewVal Then
With Sheets("Log")
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A" & LR + 1).Value = VBA.Environ("username") 'user
.Range("B" & LR + 1).Value = Now 'date and time
.Range("C" & LR + 1).Value = Sh.Name 'sheet
.Range("D" & LR + 1).Value = Target.Address(False, False) 'cell
.Range("E" & LR + 1).Value = OldVal 'previous value
.Range("F" & LR + 1).Value = Target.Value 'new value
End With
End If
cell.Select
Application.EnableEvents = True
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim LR As Long, NewVal As Variant, OldVal As Variant
Dim cell As Range
If Target.Count > 1 Then Exit Sub
If Sh.Name = "Log" Then Exit Sub
Application.EnableEvents = False
NewVal = Target.Value
Set cell = ActiveCell
On Error Resume Next
Application.Undo
OldVal = Target.Value
Target.Value = NewVal
If OldVal <> NewVal Then
With Sheets("Log")
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A" & LR + 1).Value = VBA.Environ("username") 'user
.Range("B" & LR + 1).Value = Now 'date and time
.Range("C" & LR + 1).Value = Sh.Name 'sheet
.Range("D" & LR + 1).Value = Target.Address(False, False) 'cell
.Range("E" & LR + 1).Value = OldVal 'previous value
.Range("F" & LR + 1).Value = Target.Value 'new value
End With
End If
cell.Select
Application.EnableEvents = True
End Sub
It is just to ensure that the event code is not called needlessly when the macro writes to (changes) the Log sheet.