Return to VBA Code Examples
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.
1. Put the following code somewhere in your macro to add a comment.
Sheet1.Range("A1").AddComment ("Hello World")
2. To delete a comment use this code
Sheet1.Range("A1").Comment.Delete
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.
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
Is there a way to activate a comment on a cell by hovering over it? I have a range of cells that I would like to pull respective comments from another sheet when hovered over each individual cell. The hover event would pull the comments from their respective cells in the other sheet.
The comments are of string value. Basically, I have a range of cells in Sheet 1, let’s say A1:A5 and I need comments to pop-up when I hover over them and pull from Sheet 2 range B1:B5. The reason why I won’t do it manually is because the contents of Sheet 2 change every day. That is why I am trying to see if there is a VBA solution.
asked Aug 9, 2017 at 21:09
studentofarkadstudentofarkad
1551 gold badge1 silver badge8 bronze badges
4
hovering over any cell, that contains a comment, shows that cell’s comment
this is how you add a comment to a cell and how you update the comment text
Sub aaa()
With Range("E6")
If Not .Comment Is Nothing Then .Comment.Delete
.AddComment "this is a comment"
.Comment.Text "abc123" 'No need the assignment sign "=" after .Comment.Text
End With
End Sub
answered Aug 9, 2017 at 21:24
jsotolajsotola
2,2391 gold badge9 silver badges22 bronze badges
0
Try this code.
Sub test()
Dim rngDB As Range, rngComent As Range
Dim rng As Range
Dim cm As Comment, i as integer
Set rngComent = Sheets(1).Range("a1:a5")
Set rngDB = Sheets(2).Range("b1:b5")
For Each rng In rngComent
i = i + 1
If Not rng.Comment Is Nothing Then
rng.Comment.Delete
End If
Set cm = rng.AddComment
With cm
.Visible = False
.Text Text:=rngDB(i).Value
End With
Next rng
End Sub
answered Aug 10, 2017 at 4:20
Dy.LeeDy.Lee
7,4771 gold badge11 silver badges14 bronze badges
2
A less bulky «All-in-One» solution:
Sub comment(rg As Range, Optional txt As String = "")
If rg.comment Is Nothing Then
If txt <> "" Then rg.addComment txt
Else
If txt = "" Then rg.comment.Delete Else rg.comment.text txt
End If
End Sub
Usage:
Using cell [a1]
as an example …but shortcut notation like [a1]
) should generally be avoided except for testing, etc
- Add or change comment:
comment [a1], "This is my comment!"
- Delete existing comment:
comment [a1], ""
or simplycomment [a1]
Related stuff:
- set comment box size:
[a1].comment.Shape.Width = 15
and[a1].comment.Shape.Height = 15
- set the box position:
[a1].comment.Shape.Left=10
and[a1].comment.Shape.Top=10
- change background box color:
[a1].comment.Shape.Fill.ForeColor.RGB = vbGreen
Nowadays I think that comments (or «notes», as they’re now called) are hidden by default.
-
Always show all comments:
Application.DisplayCommentIndicator=1
-
Show when mouse hovers over cell:
Application.DisplayCommentIndicator=-1
-
Disable comments (hide red indicator):
Application.DisplayCommentIndicator=0
-
show/hide individual comments like
[a1].comment.visible=true
, etc. -
get comment text:
a=[a1].comment.text
hornetbzz
9,1285 gold badges35 silver badges53 bronze badges
answered Nov 9, 2021 at 12:06
ashleedawgashleedawg
20k8 gold badges73 silver badges104 bronze badges
This code will refresh the contents of the comments every time you open the workbook. It is based on ranges of both destination as well as source. Make sure to first add a comment for the cell range first. You won’t need VBA for that.
Private Sub Workbook_Open()
Dim ws As Worksheet
Dim rg As Range
Dim comment As String
Dim i As Integer
i = 1
Set rg = Range("E1:E10") 'set range of where the comments will be seen
Set ws = Sheets("Sheet1")
For Each c In rg
comment = ws.Cells(i, 2).Value 'set location of comments you are grabbing from
c.comment.Text Text:=comment
i = i + 1
Next c
End Sub
answered Aug 9, 2017 at 21:50
TJYenTJYen
3435 silver badges13 bronze badges
1
I’ve discovered that if the sheet cell is previously formatted and contains data the VBA Add Comments routines may not work. Also, you have to refer to the cell in the «Range» («A1») format, not the «Cells» (Row Number, Column Number) format. The following short sub worked for me (utilize prior to program formatting/adding data to cell):
Sub Mod01AddComment()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim WkSheet As Worksheet
Set WkSheet = wb.Sheets("Sheet1")
Dim CellID As Range
Set CellID = WkSheet.Cells(RowNum, ColNum)
` ( or, Set CellID = WkSheet.Range("A1") )
CellID.Clear
CellID.AddComment
CellID.Comment.Visible = False
CellID.Comment.Text Text:="Comment Text"
End Sub
answered Nov 27, 2021 at 18:25
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
1 августа 2012,
VBA,
Konstantin
Создание примечания для ячейки в Excel с помощью макроса, которое также имело бы сложное внутреннее форматирование, не самая тривиальная задача. Для этого можно воспользоваться например таким кодом:
With Worksheets(1).Cells(4, 12).Comment .Visible = False .Text "Жирный шрифт:" & Chr(10) & "курсив" .Shape.DrawingObject.Characters(1, 13).Font.Bold = True .Shape.DrawingObject.Characters(15, 20).Font.Italic = True End With
В итоге получится примерно такое примечание:
2 комментария в “Создание форматированного примечания с помощью VBA”
Комментировать
VBA Macro is for developers. Macro is a piece of code written in VBA. VBA is Microsoft’s programming language and it stands for Visual Basic for Applications. Let’s see how to set up our VBA Macro and how to add comments in a VBA in Excel.
VBA Macro
Excel is a very advanced tool that contains thousands of functionalities, but VBA Macro comes into existence when we have to do a repeated task. The complex repeated tasks can be automated with the help of VBA Macro.
Initial Set-Up
Go to Developer Tab. We will see that by default developer tab is not present in the menu bar.
Follow the steps:
Step 1: Right-click on any tab in the menu bar. Click on Customize the Ribbon. A dialogue box appears.
Step 2: In the Mains Tab, check the box Developer and click on Ok.
Step 3: Now the Developer Tab is visible.
Step 4: Go to Developer Tab and click on Visual Basic.
Step 5: Now, the VBA tab is opened. Click on Tools in the menu bar and then click on Macros. A dialogue box is open.
Step 6: Write the macro name and click on create.
A Macro is created.
VBA Comments
Comments are the lines in the code that are ignored while executing the code. These are represented as green text in the code. The comments help describe the written code. Knowing the correct use of comments is very important because while working with long and complex code, comments help us identify which part of code does what. It is very helpful for development purposes.
Adding Single Line comment in VBA
Step 1: Click on the line where you want to insert a comment.
Step 2: Type an Apostrophe( ‘ ) at the start of a line.
Step 3: Write the comment you want.
Step 4: Press Enter and you fill find the comment written to be green.
Adding Multi-Line comment in VBA
We can add comments in multiple lines. We use multi-line comments when we have to add points in our description or the description is long.
Step 1: Keep your cursor on the Tool Bar.
Step 2: Right-click on the Tool Bar and click on edit. An extended Tool Bar appears. Drag and place it in the already existing Tool Bar.
Step 3: Select the text you want to comment on and click on Comment Block.
The entire selected text got commented.
Using Buttons to add a comment
Step 1: Go to Toolbar and right-click on it. A menu appears.
Step 2: Click on Customize and a dialogue box appears.
Step 3: Go to edit in the left-side scrollable list.
Step 4: Find Comment Block and Uncomment Block in the right-side scrollable list.
Step 5: Click on Comment Block and drag it to the menu bar. It will look like a button in the menu bar.
Step 6: Click on Uncomment Block and drag it to the menu bar. It will look like a button in the menu bar.
Step 7: With the dialogue box opened. Go to the comment block and right-click on it. A menu appears.
Step 8: Click inside the Name and add a character & at the starting of Comment Block. Then click somewhere outside the appeared menu.
Step 9: Again, right-click on the Comment Block and select Image and Text.
Step 10: Repeat steps 7, 8, 9 for Uncomment Block i.e. right-click on the Uncomment Block and add & in the Name. Also, select the Image and Text in the appeared menu. At last, close the dialogue box.
Step 11: A shortcut for comment and uncomment has been created in the VBA code editor. To comment on a line the shortcut is Alt + C and to uncomment a line the shortcut is Alt + U. You can also use the Comment Block and Uncomment Block buttons to comment on a line. Enter the text you want to comment on.
Step 12: To comment on the written line. You can click Alt + C.
Step 13: To uncomment a line, you can press Alt + U.
Use Rem to Comment
At the start of the comment use the keyword Rem to comment on a line.
Formatting Comments
Step 1: Go to Tools Tab, and right-click on it.
Step 2: A menu appears, and click on Options… A dialogue box appears.
Step 3: Go to Editor Format.
Step 4: Select the Comment Text from the left scrollable list.
Step 5: You can change the color of the comment by selecting Foreground. For example, red. Click Ok.
Step 6: Now, all comments will have a font color of red.