Trying to track changes using comments

Regalis

New Member
Joined
Jan 10, 2017
Messages
2
Hello all,

I am a rookie when it comes to VBAs, but I am trying to get the following code to work on the entire worksheet, not just one cell:

Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Address <> "$A$1" Then Exit Sub
If IsEmpty(Target) Then Exit Sub
Dim strNewText$, strCommentOld$, strCommentNew$
strNewText = .Text
If Not .Comment Is Nothing Then
strCommentOld = .Comment.Text & Chr(10) & Chr(10)
Else
strCommentOld = ""
End If
On Error Resume Next
.Comment.Delete
Err.Clear
.AddComment
.Comment.Visible = False
.Comment.Text Text:=strCommentOld & _
Format(VBA.Now, "MM/DD/YYYY at h:MM AM/PM") & Chr(10) & strNewText
.Comment.Shape.TextFrame.AutoSize = True
End With
End Sub


I am hoping I can make a relatively simple edit to the above code which will allow it to work on all cells that have been edited.

Any advice would be greatly appreciated!!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Craggs82

Board Regular
Joined
Jun 28, 2016
Messages
71
Hey Regalis,

all you need to do is remove..

Code:
If .Address <> "$A$1" Then Exit Sub

This line is checking to see if you're changing cell A1, if you're not it exits out of the event procedure. Removing this allows the event to trigger on any cell.

enjoy :)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
    Dim strNewText$, strCommentOld$, strCommentNew$
    
    With Target
        strNewText = .Text
        
        If Not .Comment Is Nothing Then
            strCommentOld = .Comment.Text & Chr(10) & Chr(10)
        Else
            strCommentOld = ""
        End If
        
        On Error Resume Next
        .Comment.Delete
        Err.Clear
        .AddComment
        .Comment.Visible = False
        .Comment.Text Text:=strCommentOld & _
        Format(VBA.Now, "MM/DD/YYYY at h:MM AM/PM") & Chr(10) & strNewText
        .Comment.Shape.TextFrame.AutoSize = True
    End With
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,675
Messages
5,838,712
Members
430,566
Latest member
ChanchalSingh

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
Top