Today we learn interesting new VBA code using comments in our Excel worksheet. Our data contains name of students and their marks in Math. Of course, we could also use sales or financial data. Now we have placed comments for students who need to improve their skills in Math to achieve greater scores. Let’s see how we can count the total number of comments, change the shape of the comments, display or hide the comments automatically on our Excel worksheet using VBA.
We write the following sub-routines or macros in our module. The names of the macros are self-explanatory.
Sub CommentCount()
Dim TotalComments As Integer
TotalComments = ActiveSheet.Comments.Count
MsgBox “The active worksheet has ” & TotalComments & ” comments.”, vbInformation
End Sub
Sub SelectCommentCells()
Cells.SpecialCells(xlCellTypeComments).Select
End Sub
Sub ChangeCommentShape()
Dim cmnt As Comment
For Each cmnt In ActiveSheet.Comments
If cmnt.Shape.AutoShapeType = msoShapeRectangle Then
cmnt.Shape.AutoShapeType = msoShapeRoundedRectangle
Else
cmnt.Shape.AutoShapeType = msoShapeRectangle
End If
Next cmnt
End Sub
Sub HideDisplayComments()
If Application.DisplayCommentIndicator = xlCommentAndIndicator Then
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
Else
Application.DisplayCommentIndicator = xlCommentAndIndicator
End If
End Sub
Watch the Excel training video to understand the functioning of the VBA code quickly and easily:
You can watch this video on YouTube.
Further reading: