Allow Excel VBA to Add or Delete Comments on a Protected Worksheet
Question details
The user needs Excel VBA to add and delete comments on a protected worksheet.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A VBA macro modifies comments on a worksheet protected with UserInterfaceOnly set to True.
- Observed behavior
- Deleting comments may fail without an error, while AddComment raises a runtime error.
Confirm the worksheet name and protection password, and save the workbook as a macro-enabled .xlsm file. The VBA AddComment and Comment properties discussed here refer to legacy comments, called notes in newer Excel versions.
Exclude Drawing Objects from Worksheet Protection
Use this protection setting when VBA cannot add or delete legacy comments on a protected worksheet.
Set DrawingObjects to False along with UserInterfaceOnly set to True. The worksheet remains protected, but drawing objects, including legacy comments, are not protected.
UserInterfaceOnly must be set again when the workbook opens, so place the protection code in the Workbook_Open event.
In Excel, press Alt+F11 and find your workbook in the Project Explorer.
Double-click ThisWorkbook and add the following code. Replace Sheet1 with the worksheet name and your-password with its protection password: Private Sub Workbook_Open() With ThisWorkbook.Worksheets("Sheet1") .Unprotect Password:="your-password" .Protect Password:="your-password", DrawingObjects:=False, UserInterfaceOnly:=True End With End Sub
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm), close it, and reopen it. Enable macros when prompted so the Workbook_Open code runs.
Run your macro from Developer > Macros. For a legacy comment in A1, use Range("A1").Comment.Delete to remove an existing comment and Range("A1").AddComment "Text" to add one.

Work with Excel Spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format compatibility for an easy transition. The Excel VBA protection setting above must be configured in Excel; macro compatibility in WPS can vary.

Frequently Asked Questions
Why does AddComment fail when UserInterfaceOnly is True?
UserInterfaceOnly allows macros to work with protected worksheet contents, but drawing-object protection can still block legacy comment changes. Set DrawingObjects:=False in the worksheet's Protect method.
Does UserInterfaceOnly remain set after I reopen the workbook?
No. Add the Protect call to the Workbook_Open event in ThisWorkbook and enable macros when opening the workbook.
Will users still be prevented from changing drawing objects?
No. DrawingObjects:=False leaves drawing objects unprotected, even though the worksheet itself remains protected.
Does this code apply to threaded comments?
The AddComment and Comment properties apply to legacy comments, called notes in newer Excel versions. Threaded comments use different VBA APIs.




