VBA & Macro Problems

Allow Excel VBA to Add or Delete Comments on a Protected Worksheet

Phi Hung VoPhi Hung Vo Nov 27, 2026 869 views

Question details

The user needs Excel VBA to add and delete comments on a protected worksheet.

Allow Excel VBA to Add or 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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, press Alt+F11 and find your workbook in the Project Explorer.

2
Set protection on opening

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

3
Reopen and enable macros

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.

4
Test the comment macro

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.

Exclude Drawing Objects from Worksheet Protection
Drawing objects are not protected: DrawingObjects:=False also leaves other drawing objects available for users to modify. Check whether that trade-off is acceptable before using this setting.
Free Microsoft Office alternative

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.

Open and edit Microsoft Excel .xlsx spreadsheetsReview spreadsheet data and cell notes in a familiar interfaceUse a free, lightweight office suite for everyday workMove between common Microsoft Office document formats
microsoft office alternative - wps office

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.