VBA Parent of comment

antaeusguy

Board Regular
Joined
Mar 8, 2010
Messages
81
I've written a code to change the text of all comments in a workbook to "Comment is deleted."

I got part of this code off the web and modified myself.

I am trying to understand this code: cm.Parent.AddComment

What is the parent of the object comment? Isn't it Comments? How come in this case, the parent seems to be range (as the AddComment is a member of range).

Anyone who could enlighten me on this subject would be much appreciated :)

Code:
Sub DelComments()
    Dim cm As Comment
    Dim ws As Worksheet
    
    For Each ws In Worksheets
        ws.Activate
        For Each cm In ActiveSheet.Comments
            cm.Delete
            cm.Parent.AddComment Text:="Comment is deleted."
        Next cm
    Next ws
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
What is the parent of the object comment? Isn't it Comments? How come in this case, the parent seems to be range (as the AddComment is a member of range).

Anyone who could enlighten me on this subject would be much appreciated :)

The parent of a comment is the Range object. Think of how you would have to write a comment in VBA, you can't just say

Code:
AddComment Text:="Foo"

It isn't being told where to look!

So in order to use AddComment, we have to tell it where to look, thus giving it a "Parent" object.

Code:
Range("A1").AddComment Text:="Foo"

To go further, the Worksheet object is the parent of the Range object, and the Workbook object is the parent of the Worksheet object.
 
Upvote 0

Forum statistics

Threads
1,222,560
Messages
6,166,794
Members
452,073
Latest member
akinch

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