Excel VBA Comment Function: What It Does and How to Use It

VBA & macros · Updated November 21, 2026

Excel VBA has no single “comment function”: use an apostrophe (or Rem) to add a non-running comment to VBA code, and use Range.AddComment to add a traditional note to a worksheet cell. These are different things: code comments explain a macro, while a cell note is information stored in the workbook for readers.

What “comment” means in Excel VBA

The phrase “Excel VBA comment function” can refer to either of two features. In the Visual Basic Editor, a comment is text in the source code that VBA ignores when it runs. In a worksheet, what Excel formerly called a cell comment may be a traditional note or a modern threaded comment. The right method depends on where you want the explanation to appear and whether readers need to reply.

A code comment is not an Excel worksheet function and cannot be entered in a cell to return a value. It does not change a cell, calculate a result, or display text in the grid. A cell note, by contrast, is an annotation attached to a particular cell and can be seen in the workbook without opening the VBA editor.

How to comment VBA code

In the Visual Basic Editor, put an apostrophe at the start of a line to make that line a comment. You can also place an apostrophe after a VBA statement to explain it. VBA ignores everything after the apostrophe through the end of that line. The older Rem keyword also marks a comment, but it is generally less convenient, especially for an end-of-line explanation.

Sub ExplainCommentSyntax()
    Dim message As String
    ' This line is a comment and does not run.
    Rem This is also a comment line.
    message = "The assignment runs; the comment lines do not."
End Sub

Comments are useful for explaining why code is written a certain way, identifying assumptions, or marking a section that needs maintenance. They are not a way to temporarily disable a block of code: an apostrophe comments out the rest of its line, not a selected multi-line block. In the Visual Basic Editor, use the Comment Block and Uncomment Block toolbar buttons to comment or restore selected lines. If those buttons are not visible, customize or show the relevant editor toolbar; the buttons are part of the Visual Basic Editor, not the Excel worksheet ribbon.

For Microsoft 365 on Windows, open the editor from Developer > Visual Basic, or press Alt+F11. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon and select Developer. Excel for Mac also has a Visual Basic Editor, available from Developer > Visual Basic; the keyboard shortcut can vary with the keyboard and function-key settings. Excel for the web does not run VBA macros, so use the desktop app to edit or run this code.

How to add a note to a worksheet cell

In VBA, Range.AddComment adds a traditional cell comment, now called a note in the Excel interface. For example, a macro can attach a note to cell D5. The following procedure replaces an existing traditional note in D5 before adding a new one, so it will not fail merely because that cell already has a note.

Save a copy of the workbook before running a macro. VBA changes cannot be undone with Ctrl+Z, and the example below changes workbook content. In the Visual Basic Editor, insert a standard module with Insert > Module, then paste the procedure into it.

Sub AddNoteToD5()
    Dim ws As Worksheet
    Dim target As Range

    Set ws = ThisWorkbook.Worksheets("Summary")
    Set target = ws.Range("D5")

    ' Replace an existing traditional note, if present.
    If Not target.Comment Is Nothing Then target.Comment.Delete

    target.AddComment "Reviewed on 3 November. Check the source figure."
    target.Comment.Visible = False
End Sub

Change Summary to the exact name of a worksheet in the workbook containing the macro, and change D5 to the target cell. ThisWorkbook means the workbook that contains the VBA code; it is different from ActiveWorkbook, which means the workbook currently active in Excel. Using ThisWorkbook is helpful when a macro could be run while another workbook is open.

The final line hides the note’s displayed text after it is created. A cell with a note may still show Excel’s note indicator, depending on the display settings. To show the note, set its Visible property to True. Usually, users can also hover over the cell or open the note from Excel’s Review tab.

Notes versus threaded comments

In current Excel, “comments” usually means threaded conversations: people can post replies and collaborate. Traditional comments were renamed “notes” and are suited to a short, static annotation without a reply thread. This terminology is a common source of confusion in VBA examples, many of which were written before the interface changed.

Range.AddComment creates a note, not a threaded discussion. If the requirement is a conversation with replies, use Review > New Comment in Excel for Microsoft 365 and check whether the threaded-comment VBA object model is available in the particular Excel version and environment. The object-model method Range.AddCommentThreaded is available in supported current Excel versions, but older releases and some automation environments may not support it. Do not assume that code using the traditional Comment object will create or manage a thread.

For a one-time instruction such as “Enter a date here,” a note is often sufficient. For review feedback that needs responses, a threaded comment is a better fit. If a workbook is shared with people using different Excel releases, check the behavior in those releases before relying on threaded-comment automation.

Editing and deleting a note with VBA

A traditional note can be edited through its Comment object. To replace its text, assign a new value to the object’s Text method. To remove it, call Delete. This example deletes a note in D5 only when one exists, avoiding an error if the cell has no traditional note.

Sub RemoveNoteFromD5()
    Dim ws As Worksheet
    Dim target As Range

    Set ws = ThisWorkbook.Worksheets("Summary")
    Set target = ws.Range("D5")

    ' Delete the traditional note only when it exists.
    If Not target.Comment Is Nothing Then target.Comment.Delete
End Sub

To change the text instead, use the same worksheet and range setup, test that target.Comment is not Nothing, and then call target.Comment.Text with the replacement text. Remember that this operation addresses a traditional note. A threaded comment has its own object model and should be managed as a threaded comment rather than treated as a note.

If you want to apply a note to many cells, write a loop over a clearly defined range and decide what should happen when a cell already has a note. Replacing every existing note without checking could erase useful explanations. Test a bulk procedure on a copied workbook or a small sample range first.

Common errors and how to avoid them

  • “Object variable or With block variable not set”: A cell without a traditional note returns Nothing for its Comment property. Check for Nothing before trying to edit, show, or delete that note.
  • “Application-defined or object-defined error” when adding: The target may already contain a note, or the worksheet reference may be wrong. Check the sheet name and handle an existing note before calling AddComment.
  • The note is added but not visible: The code may set Visible to False, or Excel may show notes only when selected or hovered. Check the cell and use the Review tab to display the note.
  • The code finds no comment even though the cell has a conversation: A threaded comment is not the same object as a traditional note. Use the threaded-comment features supported by the Excel version instead of relying on Range.Comment.
  • The macro does not run: Excel for the web cannot run VBA. In the desktop app, verify that macros are permitted by the organization’s security policy and that the workbook is saved in a macro-enabled format such as .xlsm.

Also check that the worksheet name is spelled exactly as it appears on the sheet tab. A reference to a missing worksheet will fail before the note is created. If the macro is in a different workbook from the data, verify whether the reference should be ThisWorkbook or another explicitly named workbook rather than relying on whichever workbook happens to be active.

Choosing the right approach

Choose based on the destination and purpose, not just on the word “comment.” A VBA source comment explains code to someone maintaining the macro. A note records a static explanation beside worksheet data. A threaded comment supports a back-and-forth discussion. These options are complementary; adding one does not automatically create the others.

  • Use apostrophe comments in VBA for explanations, assumptions, and maintenance notes.
  • Use a cell note for a concise annotation that belongs with a cell but does not need replies.
  • Use a threaded comment when collaborators need to respond or follow a discussion.
  • Use a separate worksheet or an external documentation system for long instructions, change logs, or information that must be easy to search and maintain.

For essential instructions, do not rely only on a hidden note. Put key directions in visible worksheet text, a clearly labeled input area, or documentation that users are likely to see. A note can be overlooked, and it may not be obvious to a new user that a cell contains one.

Macro safety and workbook compatibility

VBA runs with the permissions available to Excel, so only run macros from sources you trust. A procedure that adds a note is modest in scope, but macros can also change or delete data, so review code before running it. Keep a backup, especially before running a procedure across many cells. Ctrl+Z does not reverse ordinary VBA changes.

Save a workbook containing VBA as an Excel Macro-Enabled Workbook (.xlsm) if you need to preserve the macro. A standard .xlsx file does not store VBA code. Notes and threaded comments are workbook content, but support for displaying and editing them can differ in older Excel releases or other spreadsheet applications. If a workbook is shared across platforms, verify that the intended annotation type remains usable for its audience.

Frequently Asked Questions

Is there a VBA comment function? No single function is needed to comment code. Begin a line with an apostrophe, or use Rem. To put an annotation on a worksheet cell, use the appropriate note or threaded-comment feature instead.

How do I add a comment to a cell with VBA? For a traditional note, refer to the target cell and call Range.AddComment with the note text. In current Excel, this creates a note, not a threaded conversation.

What is the difference between a note and a comment in Excel? A note is the traditional, non-threaded cell annotation. A modern comment is a threaded discussion that can have replies. VBA code comments are separate from both and only document the code.

Can I comment out several VBA lines at once? An apostrophe comments out the remainder of one line. To comment or uncomment several selected lines, use the Comment Block or Uncomment Block buttons in the Visual Basic Editor; this is not a worksheet command.

Can Excel for the web run an Excel VBA comment macro? No. Excel for the web can work with workbook content, but it does not run VBA macros. Open the workbook in the Excel desktop application to edit or run VBA.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.