VBA & Macro Problems

Protect Excel Worksheets While Allowing Object Editing with VBA

Steve KSteve K Nov 22, 2026 870 views

Question details

The user wants to protect all Excel worksheets with VBA while allowing users to edit objects on protected sheets.

Protect Excel Worksheets While Allowing Object Editing with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A workbook contains multiple worksheets whose cell contents should be protected while objects remain editable.
Observed behavior
The desired result is protected worksheet contents, editable objects, and continued macro access to protected sheets.
Before you start

Open the workbook in a desktop version of Excel that supports VBA and save it as an Excel Macro-Enabled Workbook (.xlsm). If sheets are already protected, make sure you know their protection password; the example assumes the same password is used on all sheets.

Solution 1Recommended

Protect all worksheets without protecting drawing objects

Use one VBA macro to protect cell contents on every worksheet while leaving drawing objects editable.

1
Open the VBA editor

In Excel, open Developer > Visual Basic, then choose Insert > Module.

2
Add the protection macro

Paste this code into the module. Replace the empty password with the shared worksheet protection password if needed: Sub ProtectAllSheets() Dim ws As Worksheet Const sheetPassword As String = "" For Each ws In ThisWorkbook.Worksheets If ws.ProtectContents Or ws.ProtectDrawingObjects Or ws.ProtectScenarios Then ws.Unprotect Password:=sheetPassword End If ws.Protect Password:=sheetPassword, DrawingObjects:=False, Contents:=True, UserInterfaceOnly:=True, AllowFormattingCells:=True Next ws End Sub

3
Run the macro

Return to Excel, choose Developer > Macros, select ProtectAllSheets, and click Run. On a protected sheet, test editing an object and modifying a locked cell.

4
Restore macro access when reopening

In the VBA editor, double-click ThisWorkbook and add this event to reapply UserInterfaceOnly when the workbook opens: Private Sub Workbook_Open() ProtectAllSheets End Sub Save the workbook and enable macros when opening it from a trusted source.

Protect all worksheets without protecting drawing objects
Understand the protection settings: DrawingObjects:=False leaves drawing objects unprotected, while Contents:=True protects locked cells. UserInterfaceOnly:=True allows macros to modify protected sheets but must be reapplied after reopening. AllowFormattingCells:=True permits cell formatting; it does not let users change locked cell contents.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for working with Excel files. Its format compatibility helps you move spreadsheets between applications; test VBA-dependent workbooks before relying on macros in WPS.

Review and edit spreadsheets using a familiar interfaceOpen and save Microsoft Excel formats such as XLSX and XLSMMove existing spreadsheets into a lightweight Office alternativeAccess common spreadsheet tools for free
QA img-9

Frequently Asked Questions

Why can users edit objects but not locked cells?

The macro sets DrawingObjects:=False to leave drawing objects unprotected and Contents:=True to protect locked cell contents. Test the specific objects on each sheet after running Developer > Macros > ProtectAllSheets.

Why do macros lose access to protected sheets after reopening the workbook?

Excel does not retain UserInterfaceOnly protection across sessions. Add a Workbook_Open event in ThisWorkbook that calls ProtectAllSheets, and enable macros when opening the trusted workbook.

What if my worksheets have different protection passwords?

The example uses one shared password. Modify the code to supply the correct password for each worksheet before calling ws.Unprotect; otherwise, the macro will stop when it encounters a sheet it cannot unprotect.

Can users format cells without editing their contents?

Yes. The example uses AllowFormattingCells:=True, which permits cell formatting while locked cell contents remain protected. Set it to False if formatting should also be restricted.