Description:
Comments are helpful when you want to show some remarks or comments on particular cell, you can Add Clear Comments in Excel VBA.
Add Clear Comments in Excel VBA – Solution:
You can use AddComment and ClearComments methods to do this.
Example:
The following code will show you how to add and clear comments using Excel VBA.
Code:
Sub sbAddComment() 'Deletes Existing Comments Range("A3").ClearComments 'Creates Comment Range("A3").AddComment Range("A3").Comment.Text Text:="This is Example Comment Text" End Sub
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a Module for Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Press F5 to execute it
Practical Applications:
If you have lots of Cells to add comments, we general write the comments in another set of range and add using VBA.
Following is the Example program to add the comments from a range.
Sub sbAddComment_Example() For iCntr = 1 To 30 'Clear if any existing comments Range("A3").ClearComments 'Add a Comment from Column B Range("A" & iCntr).AddComment Range("A" & iCntr).Comment.Text Text:=Range("B" & iCntr).Value Next iCntr End Sub
Explantion:
- For Loop is to iterate from 1 to 30 rows, you can change as per your require mt
- ClearComents method is using to clear the existing comments if any
- AddComments method will add the comment in the particular range
- Comment.Text property is for adding the Commet text or message which you want ot show it the user on mose hover on a range
Instructions:
- Open an excel workbook
- Enter some data in Column A and B as per your requirement, to execute the above program you need to enter some data from Range A1 to Range B30.
- Press Alt+F11 to open VBA Editor
- Insert a Module for Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Press F5 to execute it
Hi,
In this code,
Sub sbAddComment_Example()
For iCntr = 1 To 30
‘Clear if any existing comments
Range(“A3”).ClearComments
‘Add a Comment from Column B
Range(“A” & iCntr).AddComment
Range(“A” & iCntr).Comment.Text Text:=Range(“B” & iCntr).Value
Next iCntr
End Sub
” Range(“A” & iCntr).Comment.Text Text:=Range(“B” & iCntr).Value” code is not working. Getting an error “Application Defined or Object Defined error” Runtime error 1004. Kindly give me correct code for this.
Thanks,
Suganthi
Hi,
Type some text in Column B1:B30 and try this code:
Sub sbAddComment_Example()
For iCntr = 1 To 30
'Clear if any existing comments
Range("A3").ClearComments
'Add a Comment from Column B
Range("A" & iCntr).AddComment
Range("A" & iCntr).Comment.Text Text:=Range("B" & iCntr).Value
Next iCntr
End Sub
Thanks-PNRao!
Plese try this one
Sub sbAddComment_Example()
For iCntr = 1 To 30
‘Clear if any existing comments
Clear A1 to A30 Clearcomments.
Range(“A” & iCntr).ClearComments
‘Add a Comment from Column B
Range(“A” & iCntr).AddComment
Range(“A” & iCntr).Comment.Text Text:=Range(“B” & iCntr).Value
Next iCntr
End Sub