Option Explicit
Dim PreviousValue As Variant ' To store the previous value of the cell
' Track the previous value of the cell when it is selected
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns("J")) Is Nothing Then
PreviousValue = Target.Value ' Store the current value before changes
Else
PreviousValue = vbNullString ' Clear if outside the target range
End If
End Sub
' Add a comment with the previous value when the content is changed
Private Sub Worksheet_Change(ByVal Target As Range)
Dim com As Comment
' Ensure changes are within the desired range
If Intersect(Target, Columns("J")) Is Nothing Then Exit Sub
' Remove the comment if it already exists, then re-add with the new value
On Error Resume Next
Set com = Target.Comment
On Error GoTo 0
If Not com Is Nothing Then
com.Delete ' Remove existing comment
End If
' Add a comment with the previous value
If Not IsEmpty(PreviousValue) And PreviousValue <> "" Then
Target.AddComment "Previous Value: " & PreviousValue
End If
End Sub