Protect an Excel Worksheet While Allowing Unlocked Cell Formatting with VBA
Question details
Allow users to format unlocked cells on a protected Excel worksheet while preserving macro access where needed.

- 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.
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.
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.
Select the cells users may edit, open Format Cells > Protection, clear Locked, and select OK.
Open Developer > Visual Basic, then use the Project window to open ThisWorkbook.
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
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.

Enable cell formatting through Excel's Protect Sheet dialog
Use the dialog when users need formatting permission but macros do not need special access to the protected sheet.
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.

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.




