Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

How to determine if a cell has a comment

Latest post Sat, Apr 19 2008 6:08 PM by Zack Barresse. 4 replies.
  • Wed, Feb 20 2008 6:16 AM

    How to determine if a cell has a comment

    Hi all, Can anybody tell me how to find out if a cell has a comment attached with it. Using VBA,:- Msgbox activecell.comment.text works ok providing that the cell has a comment. If it hasn't, run-time error 91 is generated. Thanks for any help. John
    Filed under: , , ,
    • Post Points: 53
  • Wed, Feb 20 2008 11:42 AM In reply to

    Re: How to determine if a cell has a comment

    Assign the comment to an Object  then test the Object.  Here's an example from Microsoft:

     Sub Has_Comment()
           Dim mycomment As Object

           Set mycomment = ActiveCell.Comment
           If mycomment Is Nothing Then
               MsgBox "no comment in cell"
           Else
               MsgBox mycomment.Text
           End If
       End Sub

     

    Mike Alexander Microsoft Excel MVP www.datapigtechnologies.com
    • Post Points: 5
  • Fri, Mar 14 2008 8:34 AM In reply to

    Re: How to determine if a cell has a comment

    I have this as a function which returns true or false if the nominated cell has a comment, like this:

    Public Function HasComment(ByRef rng As Excel.Range) As Boolean
        On Error Resume Next Dim cmt As Excel.Comment
        Set cmt = rng.Comment 
        If Not cmt Is Nothing Then 
             HasComment = True
        End If
    End Function

     

    Regards - Marcus

    • Post Points: 5
  • Fri, Mar 14 2008 3:15 PM In reply to

    • Alex J
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Posts 21
    • Points 349

    Re: How to determine if a cell has a comment

    You can also use Goto (F5), Special, Comments to select all comments on a sheet. Which, when I record it comes up as a single line in VBA

    Selection.SpecialCells(xlCellTypeComments).Select

    You can probably automate from there.

    Regards, Alex J

    • Post Points: 5
  • Sat, Apr 19 2008 6:08 PM In reply to

    • Zack Barresse
    • Top 100 Contributor
    • Joined on Sat, Apr 19 2008
    • Oregon, USA
    • Posts 4
    • Points 136
    • MVP

    Re: How to determine if a cell has a comment

    Hi,

     

    Or you could make a function out of it...

     

    Public Function HasComment(rngRef As Range) As Boolean
        On Error Resume Next
        HasComment = CBool(Len(rngRef.Comment.Text))
    End Function

    Filed under: ,
    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.