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!!
 

Some videos you may like

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Craggs82

Board Regular
Joined
Jun 28, 2016
Messages
56
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,531
Messages
5,602,209
Members
414,513
Latest member
junbuggle

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