Protecting a worksheet prevents accidental edits to formulas, layouts, and locked cells. VBA lets you protect and unprotect sheets programmatically — with or without a password — making it easy to lock multiple sheets at once, toggle protection from a button, or protect sheets automatically when a workbook is closed. This is far faster than clicking through the Review ribbon for each sheet individually.
This guide covers the key VBA Protect method arguments, six practical examples, and common issues. Examples include single-sheet protection, protecting all sheets in a workbook, protecting with a password, unprotecting safely, allowing specific user actions, and an auto-protect-on-close pattern.
Protect and Unprotect Syntax
The Protect and Unprotect methods belong to the Worksheet object. Both accept a Password argument. Protect also accepts optional flags that allow specific user actions even when the sheet is protected.
Examples 1–4: Protect and Unprotect in Practice
The simplest protection pattern locks the active sheet so no cells can be edited. Adding a password means users must provide it to unprotect the sheet via the ribbon or via VBA.
Looping through the Worksheets collection protects every sheet in one pass. This is much faster than clicking the Review ribbon on each sheet individually. A single password applies to all sheets equally.
By default, protection blocks all user actions including sorting and filtering. The AllowSorting and AllowFiltering flags override this selectively. Users can therefore sort and filter the data while the formula rows and column headers remain locked.
Calling Unprotect on an already-unprotected sheet causes an error if a password is provided. The ProtectContents property checks whether protection is active first. This pattern prevents unnecessary errors when macros run conditionally.
Examples 5–6: UserInterfaceOnly and Auto-Protect on Close
The UserInterfaceOnly flag is a powerful option. It protects the sheet from manual user edits while allowing VBA macros to write to locked cells without needing to unprotect first. This is ideal for dashboards that update via macros but must remain locked against accidental user changes.
The Workbook_BeforeClose event runs just before the workbook closes. Placing the protect code there ensures the sheet is always locked when the file is saved and distributed. Users who open the file always see a protected sheet, regardless of the state when it was last used.
Common Issues and How to Fix Them
Run-time error 1004 when writing to a protected sheet via VBA
This error occurs when your macro tries to write to a locked cell on a protected sheet without using UserInterfaceOnly. The quickest fix is to unprotect the sheet at the start of the macro and re-protect it at the end. Alternatively, set UserInterfaceOnly:=True when you protect the sheet. This allows VBA to write freely without needing to toggle protection on and off.
Password is forgotten — how to remove protection
Excel sheet protection uses a weak algorithm that third-party tools can remove quickly. If the password is lost, search for "Excel sheet password remover" — several free online utilities can unlock the sheet. For workbook-level passwords (File → Info → Protect Workbook), the same approach applies. Note that this removes the protection entirely, so set a new password afterwards if the sheet needs to stay protected.
Frequently Asked Questions
-
How do I protect a sheet with VBA?+Call the Protect method on a Worksheet object. For example: ActiveSheet.Protect Password:="pass123", Contents:=True. The Contents:=True argument locks all cells from editing. If you omit the password, users can unprotect the sheet via the Review ribbon without needing to enter anything. Always store the password securely — lost passwords require third-party tools to remove.
-
Can VBA write to a protected sheet without unprotecting it?+Yes, use the UserInterfaceOnly:=True parameter when calling Protect. This allows all VBA code to write to any cell without calling Unprotect first. Manual user edits via the keyboard or mouse are still blocked. Note that UserInterfaceOnly resets to False when the workbook is closed and reopened. Re-apply it in the Workbook_Open event to persist the setting.
-
How do I allow users to sort or filter a protected sheet?+Use the AllowSorting and AllowFiltering parameters when calling Protect. For example: ActiveSheet.Protect Password:="pass", AllowSorting:=True, AllowFiltering:=True. Users can then sort and filter the data normally while formula rows and header cells remain locked against editing.
-
How do I check if a sheet is already protected before unprotecting?+Use the ProtectContents property. If ws.ProtectContents is True, the sheet is protected and Unprotect is needed. If it is False, the sheet is already unlocked and calling Unprotect is unnecessary. This check prevents errors in macros that run regardless of the sheet's current protection state.