Results 1 to 8 of 8

Thread: Excel VBA: How To: Read/Write/Edit Cell COMMENTS [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Resolved Excel VBA: How To: Read/Write/Edit Cell COMMENTS [RESOLVED]

    Esteemed Forum Participants and Lurkers:
    ===============================
    Excel '97, Excel 2003 VBA

    Does anyone have any clues about how to Read and Write the COMMENT attached to a Cell? I have tried a few things directly from the HelpHeap, and they have all failed! Here are some things I have tried:
    Code:
    Dim aRange As Range
    Set aRange = ActiveWorkbook.ActiveSheet.Range("E8")
    
    'This Works!
    Range("E6").Comment.Visible = True
        
    'This generates an error if a comment already exists, but works the first time!
    '  Application defined or object defined error.
    aRange.Offset(1, 0).AddComment
        
    'This works ... but ONLY IF a comment field ALREADY EXISTS
    '  Object variable or With block variable not set
    aRange.Offset(1, 0).Comment.Text "We CHANGED this Text!"
        
    'This works whether or not a Comment already exists
    aRange.Offset(2, 0).NoteText "This is TOTAL GARBAGE!"
    How do I tell if a Comment field ALREADY EXISTS for a Cell?

    More importantly, how do I READ an existing comment so that I can edit it?

    EDIT: P.S. ... This works to Read the existing comment, but generates an error if there is no comment ...
    tStr = aRange.Comment.Text

    So, that still leaves the question of determining IF a comment already exists. Do I have to force a STUPID ERROR and then handle it? (I HATE that!).

    Thank you for any and all comments, suggestions, and assistance.
    Last edited by Webtest; Jun 26th, 2006 at 12:33 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel VBA: How To: Read/Write/Edit Cell COMMENTS ???

    Well ... Folks ...

    I've got a partial solution ... the "NoteText" offshoot from a Range Object is very helpful. It can write text into an existing comment field, or it creates a new field if a field doesn't already exist and inserts the text. It can also append text to existing text. The "999" (code below) essentially says "append the new text after the last character or after the 999th character, whichever comes first in the existing comment string".
    Code:
    Set aRange = Range("C2")
    For i = 1 To 3
        For j = 1 To 10
            aRange.Offset(j, 5).NoteText "Pass " & i & Chr(10), 999
        Next j
    Next i
    I STILL would like to know how to detect the existence of a comment without having to force an error.
    Last edited by Webtest; Jun 25th, 2006 at 02:41 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel VBA: How To: Read/Write/Edit Cell COMMENTS ???

    Hi Art
    Here's a possible solution. Its a fuction that iterates through the comments collection for a sheet to see if the .Parent property of that comment matches a passed range.
    VB Code:
    1. Function CellHasComment(ByRef ChkCell As Range) As Boolean
    2. Dim oCom As Comment
    3.    
    4.     If ChkCell.Cells.Count <> 1 Then Exit Function
    5.    
    6.     For Each oCom In ChkCell.Worksheet.Comments
    7.         If oCom.Parent = ChkCell Then
    8.             CellHasComment = True
    9.             Exit For
    10.         End If
    11.     Next oCom
    12.    
    13. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel VBA: How To: Read/Write/Edit Cell COMMENTS ???

    Thanks Declan ...

    I guess that's about as elegant as it is going to get. I guess since the Comment is an object linked to a cell and not a part of the cell itself, I can't get to it directly through a cell reference.

    I'm still amazed at the wealth of knowledge you have accumulated. Thanks for sharing.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel VBA: How To: Read/Write/Edit Cell COMMENTS [RESOLVED]

    I guess since the Comment is an object linked to a cell and not a part of the cell itself, I can't get to it directly through a cell reference
    Exactly. I really hope they change this object model in the next version of office. It would be so much nicer if the comment was a child of the cell itself.

    Always glad to help.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel VBA: How To: Read/Write/Edit Cell COMMENTS [RESOLVED]

    I Timed a loop on my machine (1.6 GHz P4) ... it takes about 30 millisec to scan 1000 comments. I'm concerned because there will be about 7,000 entries in my sheet, and everytime I add anything to the sheet I have to mess with the comments (add or append).

    At first I thought the comment idea was a good one, but now I'm thinking I'll just cram all of the comment text into a cell instead. There are advantages and disadvantages to both methods. The Popup display was the most attractive feature of the comments.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7
    New Member
    Join Date
    Dec 2010
    Posts
    1

    Re: Excel VBA: How To: Read/Write/Edit Cell COMMENTS [RESOLVED]

    I'm a novice, but I used this - though its a bit clumsy as it uses an InputBox.
    To determine if there is a comment linked to the cell and edit it:

    If ActiveCell.Comment Is Nothing Then
    newcmnt = InputBox("", , cmnt)
    ActiveCell.AddComment Text:=newcmnt
    Else
    cmnt = ActiveCell.Comment.Text
    newcmnt = InputBox("Comment", , cmnt)
    ActiveCell.Comment.Text Text:=newcmnt
    End If

  8. #8
    New Member
    Join Date
    May 2011
    Location
    Cornwall, SW England, UK
    Posts
    1

    Thumbs up Re: Excel VBA: How To: Read/Write/Edit Cell COMMENTS [RESOLVED]

    szophie you may be a novice but in this case I think you have trumped the experts! This is just what I was looking for - many thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width