XL97: Using NoteText and Comment.Text to Apply Comments

Last reviewed: March 13, 1998
Article ID: Q169767
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

In Microsoft Excel 97, the NoteText method of a cell is limited to a maximum length of 255 characters. This article explains how to work around this limitation and some of the related problems that you may encounter when you work with cell comments.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

Working Around the 255 Character Limitation of the NoteText Method

Although the NoteText method of a cell is limited to a maximum of 255 characters, you can apply a comment that is up to 32,767 characters in length to a cell. To do this, use the Text method of a Comment object that is attached to the cell. For example, instead of the following code

   ActiveCell.NoteText "This is a 1000 character comment."

use a line of code similar to the following:

   ActiveCell.Comment.Text "This is a 1000 character comment."

Using the NoteText Method with Comments Longer Than 255 Characters

If you use the NoteText method to apply a comment that is longer than 255 characters in length to a cell, the new comment is not applied to the cell, and you do not receive an error message.

For example, if you run a macro with the following code

   ActiveCell.NoteText String(256, "x")

and the active cell does not already contain a comment, no comment is added to the cell because the length of the new comment would exceed 255 characters.

If you run a macro with the following code

   ActiveCell.NoteText ActiveCell.NoteText & String(100, "x")

and the length of the existing comment exceeds 155 characters, the comment is not updated because the length of the new comment would exceed 255 characters.

NOTE: In earlier versions of Microsoft Excel, the first 255 characters of text are applied to the comment; the remaining characters are truncated.

Using Comment.Text to Change the Comment Applied to a Cell

When you use Comment.Text to change the comment applied to a cell, the cell must already have a comment applied to it. If no comment was previously applied to the cell, you receive the following error message:

   Run-time error '91':
   Object variable or With block variable not set

To apply a comment to a cell, use code similar to the following:

   ActiveCell.AddComment

NOTE: If the active cell already has a comment applied to it, you receive the following error message when you run the code:

   Run-time error '1004':
   Application-defined or object-defined error

To add a comment to a cell that may or may not contain a comment, use code similar to the following:

   Sub AddACommentToTheActiveCell()

       ' The following line suppresses the run-time error message when
       ' adding a comment to a cell.
       On Error Resume Next

       ' Add a comment to the cell. If the cell already contains a comment,
       ' this line does nothing.
       ActiveCell.AddComment

       ' Return to normal error handling.
       On Error GoTo 0

       ' Add the text to the comment. If the cell already contains a
       ' comment, this is all you actually need.
       ActiveCell.Comment.Text "This is a comment."

   End Sub

Using Comment.Text to Add New Text to an Existing Comment

If you want to add new text to an existing comment while preserving the original comment text, use a line of code similar to the following:

   ActiveCell.Comment.Text ActiveCell.Comment.Text & "New Comment Text"

Characters over the 32,767 character limit for comments are truncated.

REFERENCES

For more information on using comments in Microsoft Excel 97, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q158246
   TITLE     : XL97: How to Determine if the Active Cell Contains a Comment

   ARTICLE-ID: Q160956
   TITLE     : XL97: Cannot Remove User Name from Default Cell Comment
               Text

   ARTICLE-ID: Q156200
   TITLE     : XL97: Sounds Not Converted with Cell Notes


Additional query words: XL97 32767
Keywords : kbcode xlvbahowto xlvbainfo
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.