VBA & Macro Problems

Protect an Excel Worksheet While Allowing Unlocked Cell Formatting with VBA

Camila MilosovichCamila Milosovich Nov 27, 2026 868 views

Question details

Allow users to format unlocked cells on a protected Excel worksheet while preserving macro access where needed.

Protect an Excel Worksheet While Allowing Unlocked Cell Formatting with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet contains unlocked cells that users should be able to format, while other cells remain protected.
Observed behavior
Worksheet protection can prevent cell formatting unless the appropriate protection option is enabled.
Before you start

Identify the cells users may edit, and unlock them through Format Cells > Protection before protecting the sheet. If the sheet is already protected, unprotect it first using its password, if required.

Solution 1Recommended

Protect the worksheet with formatting enabled in VBA

Use this method when users need to format unlocked cells and macros need to modify the protected worksheet.

The AllowFormattingCells option permits formatting, while UserInterfaceOnly allows macros to modify the protected sheet without removing user restrictions. Excel does not retain UserInterfaceOnly after the workbook closes, so reapply it when the workbook opens.

1
Unlock the editable cells

Select the cells users may edit, open Format Cells > Protection, clear Locked, and select OK.

2
Open the VBA editor

Open Developer > Visual Basic, then use the Project window to open ThisWorkbook.

3
Add the protection code

Add this event procedure, replacing Sheet1 with the worksheet name and the password placeholder with your password: Private Sub Workbook_Open() With ThisWorkbook.Worksheets("Sheet1") .Protect Password:="your-password", UserInterfaceOnly:=True, AllowFormattingCells:=True .EnableSelection = xlUnlockedCells End With End Sub

4
Save and reopen the workbook

Save the file as an Excel Macro-Enabled Workbook (.xlsm), close and reopen it, and enable macros when prompted. Test formatting an unlocked cell and confirm that macros can make their intended changes.

Protect the worksheet with formatting enabled in VBA
Check the protection boundaries: AllowFormattingCells is a worksheet-wide permission, not a permission limited to unlocked cells. Restricting selection to unlocked cells helps limit what users can format, but test your worksheet's selection and formatting behavior. Worksheet protection is not a security boundary, and a password stored in VBA code can be exposed.
Free Microsoft Office alternative

Work with spreadsheets in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and compatibility with common Excel formats for an easier move between applications. Excel-specific VBA protection behavior may differ, so test macro-enabled workbooks before relying on them.

Use familiar spreadsheet formatting tools to style cellsOpen and edit common Microsoft Excel formats, including .xlsx and .xlsNavigate a familiar Office-style interfaceMove existing spreadsheets into a lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why can't I format an unlocked cell on a protected worksheet?

Unlocking a cell allows editing when the sheet is protected, but formatting requires a separate permission. Go to Review > Unprotect Sheet, then protect it again with Format cells selected.

Does AllowFormattingCells apply only to unlocked cells?

No. AllowFormattingCells is a worksheet-wide permission. Use EnableSelection = xlUnlockedCells to restrict ordinary selection to unlocked cells, and test the formatting behavior in your workbook.

Why does UserInterfaceOnly stop working after I reopen the workbook?

Excel does not save that protection setting across sessions. Reapply it in the ThisWorkbook Workbook_Open event, save the file as .xlsm, and enable macros when opening it.

How can I see the VBA syntax for my protection choices?

Use Developer > Record Macro, protect the sheet through Review > Protect Sheet, and then stop recording. Inspect the recorded code in Developer > Visual Basic, and add UserInterfaceOnly:=True if macros need that access.