Use VBA code to add and remove comments in Excel cells

Started by nandagopal, Nov 03, 2008, 07:56 PM

Previous topic - Next topic

nandagopal

To make a comment, use a Cell's Comment object. First if the Cell does not have a Comment object, usethe Cells AddComment method to create one. Then use the Comment object's Text method to assign text to it.


' Add a comment.
Private Sub cmdMakeComment_Click()
Dim rng As Range

    Set rng = ActiveSheet.Cells(4, 4)
    If rng.Comment Is Nothing Then rng.AddComment
    rng.Comment.Text "It is now " & Format(Now)
End Sub


To remove a comment, call the Comment object's Delete method.


' Remove a comment.
Private Sub cmdRemoveComment_Click()
Dim rng As Range

    Set rng = ActiveSheet.Cells(4, 4)
    If Not (rng.Comment Is Nothing) Then rng.Comment.Delete
End Sub