VBA – Add or Remove Cell Comments
This tutorial will demonstrate how to work with Cell comments in VBA.
The following code snippets show you how to add or delete a comment in a cell.
Add Cell Comment Using VBA
1. Put the following code somewhere in your macro to add a comment.
Sheet1.Range("A1").AddComment ("Hello World")
Delete Cell Comment Using VBA
2. To delete a comment use this code
Sheet1.Range("A1").Comment.Delete
Edit Cell Comment Using VBA
3. To edit a comment in a certain cell you need to read the existing comment and edit it. Use the code below
Dim OldComment As Variant
Dim NewComment As Variant
OldComment = Sheet1.Range("A1").Comment.Text
NewComment = OldComment & " Edited comment"
Sheet1.Range("A1").Comment.Delete
Sheet1.Range("A1").AddComment (NewComment)
First, you need to read the existing comment in OldComment variable.
After that you can edit a comment, e.g. add the new text to the existing one in NewComment variable.
Now, you need to delete the old comment and add the new one. In order to delete the comment, you have to use the .Comment.Delete command.
Finally, you can add the comment from the NewComment variable using the .AddComent command.