Loop through range to extract Comments (New)

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I came across this code to extract Comments (ie the New version):

Code:
https://www.mrexcel.com/board/threads/excel-vba-get-comment-from-cell.560025/

but it only worked for a single cell, D100 (which I know does indeed contains a Comment).


I adapted as follows by changing the range to C99:D101.

Code:
Dim varComment As String
Dim c As CommentThreaded

Dim rng As Range
Set rng = Sheet1.Range("C99:D101")

Dim rngelement As Range

For Each rngelement In rng.Cells
With rng
    On Error Resume Next
    Set c = .CommentThreaded
    On Error GoTo 0
    If c Is Nothing Then
        MsgBox "No comment", vbExclamation
    Else
        varComment = c.Text
        MsgBox varComment, vbInformation
    End If
End With
Next rngelement

but it failed to indicate cell D100 contained a Comment.

Can someone tell me what is wrong?

Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Your With is wrong, it's looking at the entire range, not the cell as you loop through.
 
Upvote 0
Solution
Your With is wrong, it's looking at the entire range, not the cell as you loop through.
Thanks.

I changed it to:

Code:
Dim varComment As String
Dim c As CommentThreaded

Dim rng As Range
Set rng = Sheet1.Range("C99:D101")

Dim rngelement As Range

For Each rngelement In rng.Cells
With rngelement
    On Error Resume Next
    Set c = .CommentThreaded
    On Error GoTo 0
    If c Is Nothing Then
        MsgBox "No comment", vbExclamation
    Else
        varComment = c.Text
        MsgBox varComment, vbInformation
    End If
End With
Next rngelement

and it worked as expected.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top