To protect a worksheet's formatting with VBA, use the Protect method and set the AllowFormattingCells, AllowFormattingColumns, and AllowFormattingRows arguments to True. For example: ActiveSheet.Protect Password:="mypass", AllowFormattingCells:=True. This locks the sheet against structural changes (inserting/deleting rows, renaming sheets) while still letting users apply cell formatting such as fonts, colors, and borders. You can also toggle formatting permissions on an already-protected sheet by setting the corresponding properties to True. The steps below show how to do this in the Visual Basic Editor, run the macro, and verify the result.
What You'll Need
- Excel for Windows or Mac (the VBA editor is included in all desktop versions; the steps below use the Windows interface, but Mac menus are similar).
- A workbook saved as .xlsm (macro-enabled) if you want to keep the macro.
- Basic familiarity with opening the VBA editor (Alt+F11 on Windows, or Developer > Visual Basic).
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or go to Developer > Visual Basic. If the Developer tab is not visible, right-click the ribbon and choose Customize the Ribbon, then check Developer in the right list.
- Insert a new module. In the VBA editor, go to Insert > Module. This creates a blank code window where you can paste or write your macro.
- Write the protection macro. Paste the following code into the module (adjust the sheet name and password as needed):
This protects "Sheet1" and allows users to format cells, columns, and rows. If you want to protect the active sheet instead, replace the Set line withSub ProtectSheetFormatting() Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") ws.Protect Password:="mypass", _ AllowFormattingCells:=True, _ AllowFormattingColumns:=True, _ AllowFormattingRows:=True End SubSet ws = ActiveSheet. - Run the macro. Press F5 while the cursor is inside the subroutine, or go to Run > Run Sub/UserForm. You can also close the editor and assign the macro to a button or shortcut.
- Verify the protection. Switch back to Excel. Try changing a cell's font color or applying a border—it should work. Try inserting a row or deleting a column—Excel should block it with a message that the sheet is protected. If you need to unprotect, use
ws.Unprotect Password:="mypass"in another macro or the Review tab. - Adjust permissions later. If the sheet is already protected and you want to allow formatting without unprotecting, run a macro like:
Note that calling Protect again with new arguments updates the permissions without removing protection.Sub AllowFormattingOnProtectedSheet() With ActiveSheet .EnableSelection = xlNoRestrictions .Protect Password:="mypass", AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True End With End Sub - Save the workbook. Save as .xlsm to keep the macro. If you save as .xlsx, the macro will be removed.
Common Mistakes
- Forgetting to set AllowFormatting arguments. If you omit them, they default to False, so users cannot format cells even though the sheet is protected.
- Using the wrong sheet reference.
ActiveSheetmay not be the sheet you intend if the user has selected another sheet. UseThisWorkbook.Worksheets("Sheet1")for reliability. - Not handling the password. If you hard-code a password, anyone with access to the VBA project can see it. For stronger security, use a password stored outside the workbook or prompt the user.
- Assuming protection blocks all changes. Protection only restricts what you specify. For example, if you allow formatting cells, users can still change number formats, which may affect calculations if not intended.
- Running the macro on a protected sheet without unprotecting first. If the sheet is already protected, calling Protect again with different arguments will update the permissions, but if you try to change cell contents via VBA while protected, you'll get an error. Unprotect before making structural changes in code.
Tips
- Use a descriptive password and store it securely. Avoid using the same password across multiple sheets if different permissions are needed.
- Test the macro on a copy of your workbook before applying it to important files.
- Combine with
UserInterfaceOnly:=Trueto allow your VBA code to modify the sheet while it remains protected for users. Note that this setting does not persist after the workbook is closed; you must reapply it each time the workbook opens (e.g., in the Workbook_Open event). - Document the macro's purpose in a comment or in the module's header so others (or you later) understand what it does.
- If you need to allow formatting only for specific ranges, you cannot do that with sheet protection alone; consider using locked/unlocked cells and protecting the sheet without allowing formatting, then unlocking the cells you want users to format.
Troubleshooting
- Macro doesn't run: Check that macros are enabled (File > Options > Trust Center > Macro Settings). Also ensure the workbook is saved as .xlsm.
- Formatting still blocked: Verify that the AllowFormatting arguments are set to True in the Protect call. If the sheet was protected without them, re-run the Protect method with the arguments.
- Password not accepted: Passwords are case-sensitive. If you forgot the password, you cannot easily recover it; you may need to use a password recovery tool or restore from a backup.
- Error "Method 'Protect' of object 'Worksheet' failed": This can happen if the sheet is already protected and you try to protect it again without unprotecting first, or if the password is incorrect when unprotecting. Unprotect first, then protect with new settings.
- Users can still edit cells: Sheet protection does not lock cells by default; all cells are locked but protection must be enabled. If cells are unlocked (Format Cells > Protection tab), users can edit them even when the sheet is protected. To lock all cells, select all, then check Locked in Format Cells.
Frequently Asked Questions
Can I protect a sheet without a password?
Yes, omit the Password argument: ws.Protect AllowFormattingCells:=True. This prevents accidental changes but is easy to bypass.
Does AllowFormattingCells let users change cell values?
No, it only allows formatting changes (font, color, borders, etc.). Cell values remain locked if the cells are locked and the sheet is protected.
How do I unprotect a sheet with VBA?
Use ws.Unprotect Password:="mypass". If no password was set, use ws.Unprotect.
Can I allow formatting only for certain columns?
No, sheet protection settings apply to the entire sheet. To allow formatting only in specific areas, leave those cells unlocked and protect the sheet without allowing formatting, or use a combination of unlocked cells and protected sheet.
Why does my macro stop working after reopening the workbook?
If you used UserInterfaceOnly:=True, that setting is not saved with the workbook. Reapply it in the Workbook_Open event.
Is sheet protection secure?
Excel sheet protection is not encryption; it's designed to prevent accidental changes, not to secure sensitive data. Use workbook encryption or restrict access to the file for stronger security.