Add note to the cell

ayman helmy

New Member
Joined
Mar 17, 2021
Messages
17
Office Version
  1. 365
Platform
  1. Windows
I need a vba code to insert a note in all cells in column J when this cells changes
Noting that i want it to write the note by the value that was in the cell before change
And repeat this every time the cell change without deleting the old note
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello,

If you modify cells one per one, you can do like so

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Intersect(Target, Columns("J")) Is Nothing Then Exit Sub
  UpdateCom Target
End Sub

Public Sub UpdateCom(rng As Range)
  Dim com As Comment
  Set com = rng.Comment
 
  If com Is Nothing Then
    rng.AddComment rng.Value2
  Else
    com.Text com.Text & vbCrLf & rng.Value2
  End If
End Sub
 
Upvote 0
Paste in the sheet level module :


VBA Code:
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
 
Upvote 0

Forum statistics

Threads
1,225,739
Messages
6,186,738
Members
453,369
Latest member
juliewar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top