To insert a comment in an Excel cell using a string value from another cell, you must use a VBA macro because standard Excel formulas cannot modify cell properties like comments. The most efficient method is to write a Sub procedure that loops through a range, clears existing comments to avoid errors, and assigns the .Value of a source cell to the .Comment.Text property of the target cell.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- A dataset where one column contains the target cells and another column contains the text strings you wish to convert into comments.
- The Developer tab enabled in the Excel Ribbon (if not visible, right-click any tab and select Customize the Ribbon, then check the Developer box).
- Basic familiarity with how to open the Visual Basic Editor.
Step-by-Step
- Prepare Your Data: Organize your spreadsheet so that the strings intended for the comments are in a consistent column. For example, place the values you want to see as comments in Column B and the cells that should receive those comments in Column A.
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard. This shortcut opens the VBA editor window where you can write and manage your macros. - Insert a New Module: In the top menu, go to
Insertand then selectModule. This creates a blank workspace where your code will reside, ensuring it is available to the entire workbook. - Enter the VBA Code: Copy and paste the following code into the module window. This specific script is designed to handle the common error where a cell already has a comment:
Sub ConvertStringToComment() Dim ws As Worksheet Dim cell As Range Dim sourceRange As Range Set ws = ThisWorkbook.ActiveSheet ' Define the range containing the strings you want to use as comments Set sourceRange = ws.Range("B2:B100") For Each cell In sourceRange If cell.Value <> "" Then ' Target the cell to the left (Column A) Dim targetCell As Range Set targetCell = cell.Offset(0, -1) ' Clear existing comment to prevent "Comment already exists" error If Not targetCell.Comment Is Nothing Then targetCell.Comment.Delete End If ' Add the new comment using the string value targetCell.AddComment CStr(cell.Value) End If Next cell MsgBox "Comments successfully inserted!", vbInformation End Sub - Customize the Range: Look at the line
Set sourceRange = ws.Range("B2:B100"). Change "B2:B100" to match the actual range of your string values. If your strings are in Column C and you want comments in Column B, change thecell.Offset(0, -1)to match the relative distance between columns. - Run the Macro: You can run the macro by pressing
F5while the cursor is inside the code, or by returning to the Excel sheet, clicking theMacrosbutton on theDevelopertab, selectingConvertStringToComment, and clickingRun. - Verify the Results: Hover your mouse over the target cells in Column A. You should now see the text from Column B appearing as a hover-over comment.
Common Mistakes
One of the most frequent errors is attempting to use AddComment on a cell that already contains a comment. VBA will trigger a runtime error if you try to add a comment to a cell that is already "commented." This is why the If Not targetCell.Comment Is Nothing Then targetCell.Comment.Delete block is critical; it wipes the slate clean before applying the new string.
Another common mistake is failing to handle empty cells. If your source range includes blank cells, the macro might attempt to add empty comments, which can clutter the workbook or cause unexpected behavior. Adding an If cell.Value <> "" Then check ensures that only cells with actual text are processed.
Users also frequently forget to save their workbook as an .xlsm (Excel Macro-Enabled Workbook) file. If you save the file as a standard .xlsx, all your VBA code will be stripped out upon closing, and you will have to rewrite the module the next time you open the file.
Incorrect use of the Offset property is also common. Offset(row, column) uses relative positioning. Offset(0, -1) means "stay on the same row, move one column to the left." If your source data is to the left of your target data, you must use a positive number, such as Offset(0, 1).
Tips
To make your comments more readable, you can add code to auto-size the comment box. By default, Excel creates a small, square comment box that may cut off long strings. You can add targetCell.Comment.Shape.TextFrame.AutoSize = True immediately after the AddComment line to ensure the box expands to fit the text.
If you have thousands of rows, you may notice Excel flickering or slowing down. To optimize performance, add Application.ScreenUpdating = False at the very beginning of your Sub and Application.ScreenUpdating = True at the very end. This prevents Excel from redrawing the screen after every single comment is inserted, significantly increasing execution speed.
For better organization, you can use a dynamic range instead of a hardcoded one like "B2:B100". Using Set sourceRange = ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp)) allows the macro to automatically detect how many rows of data you have, so you don't have to update the code every time your list grows.
If you want to keep the original string values but hide them from view, you can add a line at the end of the loop to change the font color of the source column to white or hide the column entirely using ws.Columns("B").Hidden = True.
Troubleshooting
If you receive a "Runtime Error 1004," it is likely because the worksheet is protected. VBA cannot modify cell properties, including adding or deleting comments, on a protected sheet. To fix this, go to the Review tab and select Unprotect Sheet before running the macro. If the sheet requires a password, you can add ws.Unprotect "YourPassword" at the start of the code and ws.Protect "YourPassword" at the end.
If the macro runs but no comments appear, check your Offset values. You might be inserting comments into a column that is hidden or outside your current view. Double-check that the sourceRange accurately reflects where your text is located.
If the VBA editor says "Compile Error: Variable not defined," you likely have Option Explicit turned on at the top of your module. This is actually a good practice, but it means every variable must be declared. Ensure you have Dim ws As Worksheet, Dim cell As Range, and Dim targetCell As Range at the top of your procedure.
If the comments are appearing but the text is cut off, ensure you have implemented the AutoSize = True tip mentioned above. Without this, Excel uses a default box size that rarely fits long descriptive strings.
Frequently Asked Questions
Can I do this without VBA using a formula?
No. Standard Excel formulas (like IF or VLOOKUP) can only return values to a cell; they cannot change the formatting or metadata of a cell, such as adding a comment or changing the cell color.
Will this work on Excel for Web?
No, VBA macros do not run in the web browser version of Excel. You must open the file in the Excel Desktop Application to execute the macro. Once the comments are inserted and the file is saved, they will be visible in the web version.
Can I insert comments from a different worksheet?
Yes. Instead of Set ws = ThisWorkbook.ActiveSheet, you can specify the sheets by name, such as Set sourceSheet = ThisWorkbook.Sheets("Data") and Set targetSheet = ThisWorkbook.Sheets("Summary"), then reference them accordingly in the loop.
Is there a limit to how many comments I can add?
While there is no hard limit on the number of comments, adding tens of thousands of comments can significantly increase the file size and may cause the workbook to lag during scrolling.
Can I format the text inside the comment via VBA?
Yes, you can access the Comment.Shape.TextFrame.Characters property to change the font, size, or color of the text within the comment box using additional VBA lines.