Hi VoG,
I would like to use tihs code in one of my reports, is it possible to exclude certain cells from being included? I have a range called "Data" that I would like to exclude.
Also is it possible for the previous contents before the change to be included? so there is a before and after.
Regards Damian
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim LR As Long
If Sh.Name = "Log" Then Exit Sub
Application.EnableEvents = False
With Sheets("Log")
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A" & LR + 1).Value = VBA.Environ("username")
.Range("B" & LR + 1).Value = Now
.Range("C" & LR + 1).Value = Sh.Name
.Range("D" & LR + 1).Value = Target.Address(False, False)
.Range("E" & LR + 1).Value = Target.Value
End With
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
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
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
Application.EnableEvents = True
End Sub