+ Reply to Thread
Results 1 to 5 of 5

Function to return comment from a cell, or null string if no comment

Hybrid View

  1. #1
    Registered User
    Join Date
    02-10-2013
    Location
    Frankfurt, Germany
    MS-Off Ver
    Excel 2003
    Posts
    57

    Function to return comment from a cell, or null string if no comment

    Hi,

    This seems like it should be easy, but I'm struggling with it. I'm trying to make a function which will return a comment from a specified cell, or a null string if the cell doesn't have a comment. I wrote this, and it returns the actual comment fine, but it's returning a #VALUE error instead of the desired null string when applied to a non-commented cell:

    Function GetComment(CommentedCell As Range) As String
    
    If IsError(CommentedCell.Comment.Text) = True Then
        GetComment = vbNullString
    Else
        GetComment = CommentedCell.Comment.Text
    End If
    
    End Function
    I know that I can use "IfError" on worksheets, but I need this to work inside another sub. So it needs to return the blank string.

    Thanks a lot for reading!

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Function to return comment from a cell, or null string if no comment

    Maybe this

    Function GetComment(CommentedCell As Range) As String
     On Error GoTo nocomment
     If Not CommentedCell.Comment.text = "" Then
        GetComment = CommentedCell.Comment.text
     End If
    Exit Function
    nocomment:
        GetComment = "Cell Has No Comment"
    End Function
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Registered User
    Join Date
    02-10-2013
    Location
    Frankfurt, Germany
    MS-Off Ver
    Excel 2003
    Posts
    57

    Re: Function to return comment from a cell, or null string if no comment

    Thanks so much, that's working great. I didn't know about On Error.

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Function to return comment from a cell, or null string if no comment

    No problem. Thanks for the Rep!

  5. #5
    Valued Forum Contributor Maudibe's Avatar
    Join Date
    12-21-2016
    Location
    USA
    MS-Off Ver
    2010-2013
    Posts
    326

    Re: Function to return comment from a cell, or null string if no comment

    Mike,

    you can also use:

    If CommentedCell.Comment is nothing then
    Function GetComment(CommentedCell As Range) As String
     On Error GoTo nocomment
     If  Not CommentedCell.Comment is Nothing then
        GetComment = CommentedCell.Comment.text
     End If
    Exit Function
    nocomment:
        GetComment = "Cell Has No Comment"
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Show comment on mouse over cell rather than over comment icon....?
    By BellyGas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-16-2015, 09:05 AM
  2. Check if cell in range is empty, if it is delete comment else format comment to
    By banaanas in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-15-2015, 01:15 AM
  3. [SOLVED] Return Value from cell comment
    By rodgersmg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-20-2014, 04:37 AM
  4. Cell's text / comment (no comment )
    By Marianus in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-22-2013, 06:05 AM
  5. Disable Delete Comment, Edit Comment, Show/Hide Comment
    By Shanthan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2013, 06:12 AM
  6. open a comment to cell range upon true condition then removing comment on false condition
    By ferrum_equitis in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-08-2012, 12:55 AM
  7. Cell Comment Truncating String?
    By bstubbs in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-11-2009, 03:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1