Appending an existing comment with vba

Jockster

Board Regular
Joined
Jan 16, 2009
Messages
88
The following code will add a comment to the cell to the right:

Private Sub Worksheet_Change(ByVal Target As Range)
'adds a comment box to cells in columns J, K & L when dates added to cells to the left of them
On Error Resume Next
If Not Intersect(Target, Me.Range("I6:K30")) Is Nothing Then
With Target
If .Value <> "" Then
Application.EnableEvents = False
Target.Offset(0, 1).AddComment.Text Text:="Due date: " & Target.Value + 14
On Error GoTo 0
Application.EnableEvents = True
End If
End With
End If
End sub

My question is, using code, how can I a) check if there is a comment already associated to the cell in question and then b) if there is, append that comment with "Target.Value + 28"?

Thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'   adds a comment box to cells in columns J, K & L when dates added to cells to the left of them
    Dim x As Comment
    On Error Resume Next
    If Not Intersect(Target, Me.Range("I6:K30")) Is Nothing Then
        With Target
            If .Value <> "" Then
                Application.EnableEvents = False
                With Target.Offset(0, 1)
                    Set x = .Comment
                    If x Is Nothing Then
                        .AddComment.Text Text:="Due date: " & Target.Value + 14
                    Else
                        .Comment.Text .Comment.Text & vbLf & "Due date: " & Target.Value + 14
                    End If
                End With
                On Error GoTo 0
                Application.EnableEvents = True
            End If
        End With
    End If
End Sub
 
Upvote 0
Similar but with the due date formatted

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'adds a comment box to cells in columns J, K & L when dates added to cells to the left of them
If Not Intersect(Target, Me.Range("I6:K30")) Is Nothing Then
    With Target
        If .Value <> "" Then
            Application.EnableEvents = False
            With .Offset(0, 1)
                If .Comment Is Nothing Then
                    .NoteText Text:="Due date: " & Format(Target.Value + 14, "dd/mm/yy")
                Else
                    .NoteText Text:=.NoteText & vbCrLf & "Due date: " & Format(Target.Value + 14, "dd/mm/yy")
                End If
            End With
            Application.EnableEvents = True
        End If
    End With
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

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