Excel Track Comments VBA: Automate Review Notes
In this article
Tracking comments in Excel using VBA allows you to automatically log, count, or export reviewer notes without manual copying. This is particularly useful for audit trails, quality control, and summarizing feedback from multiple collaborators. To achieve this, you will use the `Comment` object within the `Range` class in the Visual Basic for Applications (VBA) editor. The process involves iterating through cells that contain comments, extracting the text, author, and timestamp, and then writing this data to a new worksheet or external file.
What You'll Need
- Microsoft Excel installed on your computer (Windows or Mac).
- A workbook containing existing cell comments (legacy comments or threaded comments may behave differently; this guide focuses on standard legacy comments which are most common in VBA scripts).
- Basic understanding of how to open the VBA Editor (Alt + F11).
- Trust access to the VBA project object model enabled if running macros from other sources, though local execution usually requires only enabling macros.
Step-by-Step
- Prepare Your Workbook: Open the Excel file containing the comments you wish to track. Ensure that the comments are standard "Notes" or "Comments" (right-click a cell > Insert Comment). Note that newer "Threaded Comments" introduced in recent Excel versions are not directly accessible via the traditional `Cell.Comment` property in older VBA libraries; they require different handling or conversion to legacy format for simple extraction.
- Open the VBA Editor: Press
Alt + F11on Windows orOption + F11on Mac to launch the Microsoft Visual Basic for Applications window. - Insert a New Module: In the Project Explorer pane, right-click on any module or folder under your workbook's name. Select Insert > Module. This creates a blank code container where you can write your subroutine.
- Write the Extraction Code: Copy and paste the following VBA code into the new module. This script loops through all used cells in the active sheet, checks for comments, and writes the details to a new sheet named "CommentLog".
- Run the Macro: Place your cursor inside the `TrackComments` sub. Press
F5to run it. Alternatively, close the VBA editor, go to the View tab, click Macros, select `TrackComments`, and click Run. - Review the Output: A new sheet named "CommentLog" will appear at the end of your workbook. It will list every cell address that had a comment, the author's name, the full text of the comment, and the timestamp of when the macro was executed.
Sub TrackComments()
Dim ws As Worksheet
Dim cmtWs As Worksheet
Dim rng As Range
Dim cell As Range
Dim r As Long
' Set the source worksheet
Set ws = ActiveSheet
' Create or clear the destination worksheet
On Error Resume Next
Set cmtWs = ThisWorkbook.Sheets("CommentLog")
If cmtWs Is Nothing Then
Set cmtWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
cmtWs.Name = "CommentLog"
Else
cmtWs.Cells.Clear
End If
On Error GoTo 0
' Add headers
cmtWs.Range("A1").Value = "Cell Address"
cmtWs.Range("B1").Value = "Author"
cmtWs.Range("C1").Value = "Comment Text"
cmtWs.Range("D1").Value = "Date Created"
r = 2 ' Start row for data
' Loop through each cell in the used range
For Each cell In ws.UsedRange
If Not cell.Comment Is Nothing Then
cmtWs.Cells(r, 1).Value = cell.Address
cmtWs.Cells(r, 2).Value = cell.Comment.Author
cmtWs.Cells(r, 3).Value = cell.Comment.Text
' Note: DateCreated is available in some versions but often unreliable.
' Using Now() as a proxy for when the macro ran, or omitting if strict accuracy needed.
cmtWs.Cells(r, 4).Value = Now()
r = r + 1
End If
Next cell
MsgBox "Comments tracked successfully!", vbInformation
End Sub
Common Mistakes
One frequent error is attempting to access `Threaded Comments` using the `Cell.Comment` property. In modern Excel, threaded comments are stored differently. If your comments do not appear in the log, check if they are threaded. You may need to convert them to legacy comments by right-clicking the cell and selecting "Convert to Legacy Comment" before running the script. Another mistake is forgetting to handle empty comments; while rare, a comment object might exist with no text, causing minor formatting issues in the output. Always ensure your destination sheet name does not conflict with an existing sheet unless you intend to overwrite it, as shown in the error-handling block of the code.
Tips
To make this tool more robust, consider adding a filter for specific authors. You can modify the `If Not cell.Comment Is Nothing Then` condition to include `And cell.Comment.Author = "TargetName"`. This allows you to track only comments from specific reviewers. Additionally, instead of logging to a new sheet, you could append the data to a CSV file using the `FileSystemObject` in VBA. This is ideal for archiving comments over time without cluttering the main workbook. Remember to save your workbook as an `.xlsm` (Macro-Enabled Workbook) file so that the VBA code persists after closing Excel.
Troubleshooting
If the macro runs but produces no results, verify that the comments are indeed attached to cells and not just floating shapes. Right-click a known commented cell and choose "Edit Comment" to confirm it is a true cell comment. If you receive a "Runtime Error 91: Object variable or With block variable not set," ensure that the `Active Sheet` has data in its `UsedRange`. Sometimes, clearing formats and re-saving the file can reset the `UsedRange` correctly. If the macro fails to create the new sheet, check if a sheet named "CommentLog" already exists and is protected; unprotect it or change the name in the code. Finally, if security settings block the macro, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and enable "Enable all macros" for testing purposes, though this should be disabled for general security.
Frequently Asked Questions
Can VBA read threaded comments?
No, the standard `Cell.Comment` property does not support threaded comments introduced in Excel 365. Threaded comments require the `CommentThread` object, which is part of a newer API and may not be available in all Excel versions. For reliable tracking, convert threaded comments to legacy comments first.
How do I delete comments after tracking them?
You can add a line `cell.Comment.Delete` inside the loop after recording the data. Be cautious, as this permanently removes the comment from the original cell. It is recommended to back up your workbook before running deletion scripts.
Does this work on Mac Excel?
Yes, the VBA logic works on Mac Excel, but keyboard shortcuts differ (use Option + F11). Some advanced features like `FileSystemObject` may have limitations on Mac due to sandboxing, but writing to a new worksheet within the same workbook works fine.
Why is the date column showing the current time?
The `Cell.Comment` object does not reliably store the creation date in many Excel versions. The code uses `Now()` to record when the macro ran. To get actual creation dates, you would need complex parsing of XML files within the .xlsx package, which is beyond basic VBA scope.
Can I track comments across multiple sheets?
Yes, wrap the main loop in another `For Each ws In ThisWorkbook.Worksheets` loop. Exclude the "CommentLog" sheet from the iteration to prevent recursive errors. This allows you to generate a master log for the entire workbook.