Hello,
Setup: I use the two macros below to log changes (i.e. keep an audit trail) in a worksheet (using Excel's "Track changes" tool forces me to share the workbook...and sharing comes with loads of limitation...).
Issue: It works great except for one thing: if I paste new data over a range a cells (i.e. variable "Target" contains more than one cell), the 2nd macro variable "Previous value" only returns the value of the first cell over which new data was pasted.
Question: How can I modify the code so that variable "Previous value" is stored as an array of all the selected cells in the first macro and retreived as such in the second macro?
Thanks!
Setup: I use the two macros below to log changes (i.e. keep an audit trail) in a worksheet (using Excel's "Track changes" tool forces me to share the workbook...and sharing comes with loads of limitation...).
Issue: It works great except for one thing: if I paste new data over a range a cells (i.e. variable "Target" contains more than one cell), the 2nd macro variable "Previous value" only returns the value of the first cell over which new data was pasted.
Question: How can I modify the code so that variable "Previous value" is stored as an array of all the selected cells in the first macro and retreived as such in the second macro?
Thanks!
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Store current value of seleted cell
Application.EnableEvents = True
PreviousValue = Target.Value
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long
Dim c As Range
On Error Resume Next
Application.EnableEvents = False
If Target.Value <> PreviousValue Then
With Sheets("LogSheet")
For Each c In Target
LR = .Cells(Rows.Count, "A").End(xlUp).Row + 1
.Cells(LR, "A").Value = ActiveSheet.Name & "!" & c.Address
.Cells(LR, "B").Value = Now
.Cells(LR, "C").Value = Environ("UserName")
.Cells(LR, "D").Value = PreviousValue
.Cells(LR, "E").Value = c.Value
Next c
End With
End If
Application.EnableEvents = True
End Sub