Aloha all,
I’m looking for a VBA code snippet to determine if a comment already exists in a cell before I add one. Seems simple, but I’m spinning my wheels.
Mahalo in advance for your comments on comments.
JohnJ
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » How to determine if a comment already exists? (Excel 97 SR-2)
The comments object is part of the range of a given worksheet. Cycle through this object for a given worksheet to test if it has any comments or to add/remove comments. If you write a comment to a cell that already has one, it’ll replace the previous comment. Here’s code that works in xl2000 that determines if for a selected range if there are comments:
Public Function HasComment(myRng As Range) As Boolean
Dim cmt As Comment, rng As Range
Debug.Print “# of comments on sheet: ” & ActiveSheet.Comments.Count
For Each cmt In ActiveSheet.Comments
Debug.Print cmt.Text
Next cmt
‘ get range of cells in worksheet that have comments
Set rng = myRng.SpecialCells(xlCellTypeComments)
If rng.Cells.Count > 0 Then
If Not Intersect(rng, myRng) Is Nothing Then
Debug.Print “Yes, there are comments in range: ” & myRng.Address
Else
Debug.Print “No comments in range: ” & myRng.Address
End If
End If
End Function
You can edit it for any worksheet and replace the debug statements with real code.
HTH, Deb
I hit POST IT too soon… The code works but has an extra, unnecessary line in it. Once you find the range of cells that have comments in the given worksheet, you don’t need to do the “if rng.cells.count” I just added that before I did the Intersect, not needed. Do with it as you will.
Deb
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications