Name

comment.Text([Text], [Start], [Overwrite])

Synopsis

Gets or sets the text displayed in a comment.

Argument

Settings

Text

The text to display in the comment.

Start

The character position at which to insert the text. If omitted, any existing comment is overwritten.

Overwrite

True replaces the existing comment; False appends. Default is False.

The following code creates four new comments on the active worksheet:

Sub CreateComments(  )
    Dim cel As Range
    For Each cel In [a1:b2]
        cel.AddComment "Comment for " & cel.Address
    Next
End Sub

The Text method also returns the text of the comment. For example, the following code lists all of the comments on the active sheet:

Sub ShowComments(  )
    Dim cmt As Comment
    Debug.Print "Author", "Text"
    For Each cmt In ActiveSheet.Comments
        Debug.Print cmt.Author, cmt.Text
    Next
End Sub

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.