The Comment Object

Recall that the AddComment method of the Range object is used to add a comment to a range. Once the comment has been added, a corresponding Comment object is created. Each comment object belongs to the Comments collection of the Worksheet object.

To illustrate, the following code creates a comment in cell A1 if it does not already exist. It then sets the text and makes the comment visible for approximately three seconds. Note the use of the DoEvents statement to ensure that Windows has the opportunity to display the comment before entering the Do loop. (You might want to try this code without the DoEvents statement. On my system, the comment is not displayed.) Note also that the Timer function returns the number of seconds since midnight (so there is a potential problem if the three-second interval happens to occur at midnight).

Dim tm As Single tm = Timer

If Range("A1").Comment Is Nothing Then

Range("A1").AddComment "comment" End If

Range("A1").Comment.Text "Created: " & Now

Range("A1").Comment.Visible = True DoEvents

Do: Loop Until Timer - tm > 3 Range("A1").Comment.Visible = False

0 0

Post a comment