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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,986
Members
448,538
Latest member
alex78

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