Loop through range to extract Comments (New)

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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,215,078
Messages
6,122,997
Members
449,093
Latest member
masterms

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