Share via


How to: Add and Delete Worksheet Comments

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Excel 2003

  • Excel 2007

For more information, see Features Available by Application and Project Type.

You can programmatically add and delete comments in Microsoft Office Excel worksheets. Comments can be added only to single cells, not to multi-cell ranges.

Adding and Deleting a Comment in a Document-Level Project

The following examples assume that there is a single-cell NamedRange control named dateComment on a worksheet named Sheet1.

To add a new comment to a named range

  • Call the AddComment method of the NamedRange control and supply the comment text. This code must be placed in the Sheet1 class.

    Me.dateComment.AddComment("Comment added " & DateTime.Now)
    
    this.dateComment.AddComment("Comment added " + DateTime.Now.ToString());
    

To delete a comment from a named range

  • Verify that a comment exists on the range and delete it. This code must be placed in the Sheet1 class.

    If Not Me.dateComment.Comment Is Nothing Then 
        Me.dateComment.Comment.Delete()
    End If
    
    if (this.dateComment.Comment != null)
    {
        this.dateComment.Comment.Delete();
    }
    

Adding and Deleting a Comment in an Application-Level Add-In Project

The following examples assume that there is a single-cell Range named dateComment on the active worksheet.

To add a new comment to an Excel range

  • Call the AddComment(Object) method of the Range and supply the comment text.

    Dim dateComment As Excel.Range = Me.Application.Range("A1")
    dateComment.AddComment("Comment added " & DateTime.Now)
    
    Excel.Range dateComment = this.Application.get_Range("A1", missing);
    dateComment.AddComment("Comment added " + DateTime.Now.ToString());
    

To delete a comment from an Excel range

  • Verify that a comment exists on the range and delete it.

    Dim dateComment As Excel.Range = Me.Application.Range("A1")
    If Not dateComment.Comment Is Nothing Then
        dateComment.Comment.Delete()
    End If
    
    Excel.Range dateComment = this.Application.get_Range("A1", missing);
    if (dateComment.Comment != null)
    {
        dateComment.Comment.Delete();
    }
    

See Also

Tasks

How to: Display Worksheet Comments

Concepts

Working with Worksheets

NamedRange Control

Change History

Date

History

Reason

July 2008

Added a code example that can be used in an application-level add-in.

Customer feedback.