Adding a new Comment object

You may have noticed that the list of methods for the Comment object doesn't include a method to add a new comment. This is because the AddComment method belongs to the Range object. The following statement adds a comment (an empty comment) to cell A1 on the active worksheet:


If you consult the Help system, you discover that the AddComment method takes an argument that represents the text for the comment. Therefore, you can add a comment and then add text to the comment with a single statement, like this:

Range("A1") .AddComment "Formula developed by JW. "

Note The AddComment method generates an error if the cell already contains a comment. To avoid the error, your code can check whether the cell has a comment before adding one.

CD- If you'd like to see these comment object properties and methods in action, check out the ROM example workbook on the companion CD-ROM. This workbook, named ® comment object.xlsm, contains several examples that manipulate Comment objects with VBA code. You probably won't understand all the code, but you will get a feel for how you can use VBA to manipulate an object.



0 0

Post a comment