You insert a comment in Excel with VBA by calling the AddComment method on a Range object: Range("B2").AddComment "Check this figure.". That single line attaches a note (called a legacy comment, or a threaded comment in newer versions) to the cell. The one catch: if the cell already has a comment, the code errors unless you delete it first. Everything below shows how to run the code, how to clear old notes, loop through many cells, and fix the usual failures.
What You'll Need
- Microsoft Excel for Windows or Mac with macros enabled. The steps below use the VBA editor, which is the same editor on both platforms, though menu names can differ slightly by version and region.
- A workbook saved as .xlsm (macro-enabled) if you want to keep your code. .xlsx files discard macros on save.
- Macro security that allows you to run code: check File > Options > Trust Center > Trust Center Settings > Macro Settings on Windows, or Excel > Security on Mac.
- Basic comfort with pressing Alt+F11 (Visual Basic Editor) and typing a short procedure. No programming experience beyond that is required.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 on Windows. On Mac, use Tools > Macro > Visual Basic Editor (the key combination varies, so the menu is safer). A separate window appears with a Project Explorer on the left.
- Insert a module. In the editor, choose Insert > Module. A blank white code window opens. If the Project Explorer is hidden, use View > Project Explorer first.
- Check the comment support in your version. Right-click a single cell on the sheet and look at the menu. If you see New Note, your version uses threaded comments and the older style is called a note;
AddCommentstill adds a note-style comment. If you see Insert Comment, you have the classic comment type. Either way the code below works, but the wording in the Excel ribbon will match whichever label you saw. - Write a simple procedure. In the module, type this, replacing the cell and text as needed:
Sub AddOneComment() Range("B2").AddComment "Please verify this total." End SubThe text after the method name is the comment body. You do not need quotation marks around a cell reference. - Handle cells that already have a comment.
AddCommentfails on an occupied cell, so clear it first:Sub SafeComment() With Range("B2") If Not .Comment Is Nothing Then .Comment.Delete .AddComment "Please verify this total." End With End SubThis checks whether a comment exists, removes it, then adds the new one. Note that the text is overwritten, so this is not additive. - Run the macro. Press F5 with the cursor inside the procedure, or return to Excel and use Developer > Macros, select the procedure, and click Run. If the Developer tab is not visible, enable it in File > Options > Customize Ribbon (Windows) or Excel > Preferences (Mac).
- Verify the result. Look for a small red indicator in the corner of the cell; hover over it to read the note. On newer versions it may appear as a small purple or grey marker that opens a pane when clicked.
- Comment several cells at once. Loop through a range rather than writing one line per cell:
Sub AddManyComments() Dim c As Range For Each c In Range("B2:B10") If Not c.Comment Is Nothing Then c.Comment.Delete c.AddComment "Reviewed " & Format(Date, "yyyy-mm-dd") Next c End SubThis is the practical way to label a whole column without repeating code. - Add a comment to a specific sheet by name. If the wrong sheet is active, reference it explicitly so the code lands where you expect:
Worksheets("Sheet1").Range("C5").AddComment "Source: Q3 report"Substitute your real sheet name in quotes. Names with spaces need the quotes; they are required here either way. - Save the workbook. Choose File > Save As and keep the macro-enabled format so the procedure survives. Saving as plain .xlsx prompts a warning and drops the code.
Common Mistakes
- Assuming
AddCommentappends. It does not. Calling it on a cell with an existing comment produces a run-time error. Always check with.Comment Is Nothingfirst. - Putting the code in the wrong place. Code pasted into a sheet module or the workbook module can behave differently from a standard module. Insert > Module keeps it simple.
- Forgetting the target sheet.
Range("B2")always refers to the active sheet. If another sheet is selected when the macro runs, the comment appears there instead. - Saving as .xlsx. The macro disappears the next time you open the file. Use .xlsm from the start.
- Confusing notes and threaded comments.
AddCommentcreates the older note style. Threaded comments (the modern kind with replies) are handled with a different object model and are not created by this method. - Using an unquoted sheet name with spaces.
Worksheets(Sheet 1)is invalid;Worksheets("Sheet 1")is correct.
Tips
- Use
Format(Date, "yyyy-mm-dd")inside the comment text to stamp review dates automatically in a sortable form. - Keep comment text short. Long notes get truncated visually and are hard to read on a hover.
- Store the comment message in a cell on a settings sheet and read it with
Range("Settings!B1").Valueso you can edit the wording without touching code. - Turn off screen updating with
Application.ScreenUpdating = Falseat the top and set it back toTrueat the end when adding hundreds of comments. - Add
Option Explicitat the top of the module so undeclared variables are caught before they cause silent errors.
Troubleshooting
"Application-defined or object-defined error" usually means the cell already has a comment, the sheet name is wrong, or no workbook is open. Clear the existing comment, check the name, and confirm a workbook is active.
"Run-time error 1004" can also come from an invalid range address such as Range("B2:B"). Fix the address to a complete reference like B2:B10.
Nothing happens when you run it if the code went into a sheet module but the macro list shows only procedures in standard modules. Move it with Insert > Module, or call it by the correct name.
No red indicator appears if the sheet has comment indicators switched off. Check File > Options > Advanced > Display, or the Mac equivalent, and turn indicator display back on. It may also be a modern comment that opens in a pane instead.
The macro is gone after reopening because the file was saved as .xlsx. Save As again choosing the macro-enabled format, then re-add the procedure if necessary.
Macros are disabled if a security warning bar appears above the sheet. Adjust the Trust Center macro settings only for files you created or trust, since enabling macros on unknown workbooks is a real security risk.
Frequently Asked Questions
Can I add a threaded comment instead of a note with VBA?
Not with AddComment. That method creates the classic note style. Threaded comments use a different object model, and scripted creation of them is limited; for a few cells, adding them by hand is more reliable.
How do I change the comment text after it exists?
Set the text property: Range("B2").Comment.Text "New wording". This replaces the body but keeps the comment in place.
How do I delete all comments on a sheet with VBA?
Use Cells.ClearComments on the target sheet, or loop the used range and delete each comment. Reference the sheet explicitly to avoid clearing the wrong one.
Does this work in Excel for the web?
No. The online version does not run VBA, so you must open the workbook in the desktop application to run or edit the macro.
Can I format comment text, such as making it bold?
The comment's text shape supports some formatting, but it is fiddly and version-dependent. For most uses, plain text is the practical choice.
Will the comments show when I print the sheet?
They print only if you choose to print comments. In Page Layout > Print Titles > Comments, select where they should appear. The default is not to print them.