Excel VBA: Unprotect and Protect Sheets
In this article
To unprotect and protect a worksheet in Excel VBA, use the Unprotect and Protect methods on the Worksheet object: Worksheets("Sheet1").Unprotect Password:="yourpassword" to remove protection, then Worksheets("Sheet1").Protect Password:="yourpassword" to reapply it. This lets your macros make changes that would otherwise be blocked, then restore protection so users cannot edit locked cells. You need the worksheet password (if one was set) and a workbook saved as .xlsm or .xlsb to store the macro.
What You'll Need
- Excel desktop (Windows or Mac) with macros enabled. The VBA editor is not available in Excel for the web.
- The password for the protected sheet, if one was set. Without it, VBA cannot unprotect the sheet.
- A workbook saved in a macro-enabled format (.xlsm) or binary format (.xlsb).
- Basic familiarity with opening the VBA editor (Alt+F11 on Windows, or the Developer tab > Visual Basic).
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or go to Developer > Visual Basic. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to enable it. Menu labels can differ slightly by Excel version and platform.
- Insert a module. In the Project Explorer, right-click your workbook, choose Insert > Module. This is where you will paste the code.
- Write the unprotect line. Type
Worksheets("Sheet1").Unprotect Password:="yourpassword". Replace Sheet1 with the actual sheet name (the tab name, not the code name) and yourpassword with the real password. If the sheet has no password, useWorksheets("Sheet1").Unprotectwith no argument. - Add your editing code. Between the unprotect and protect lines, put whatever the macro needs to do, for example writing a value:
Worksheets("Sheet1").Range("A1").Value = "Updated". Any action that changes locked cells will fail if the sheet is still protected. - Write the protect line. Type
Worksheets("Sheet1").Protect Password:="yourpassword". This reapplies protection with the same password. You can add optional arguments such asAllowFiltering:=TrueorUserInterfaceOnly:=Trueif you need specific permissions, but keep it simple first. - Run the macro. Press F5 while the cursor is inside the subroutine, or go to Developer > Macros and select your macro. Watch for errors; if the password is wrong, Excel raises run-time error 1004.
- Verify the result. Check that the sheet is protected again (the Protect Sheet button on the Review tab should show Unprotect Sheet) and that your edit was applied.
- Save the workbook as .xlsm so the macro persists. A standard .xlsx save will strip the code.
Common Mistakes
- Wrong sheet reference.
Worksheets("Sheet1")uses the tab name. If the tab was renamed, the code fails. Use the code name (e.g.,Sheet1without quotes) if you want it to survive tab renames. - Missing or incorrect password. VBA cannot bypass a password. If you do not know it, you must unprotect manually with the correct password or restore from a backup.
- Forgetting to re-protect. If the macro errors between the unprotect and protect lines, the sheet stays unprotected. Use error handling (see Tips).
- Protecting with different options. If the original protection allowed filtering or formatting, calling
Protectwith no arguments resets those permissions. Specify the same options you need. - Saving as .xlsx. Macros are lost. Always use .xlsm.
- Assuming UserInterfaceOnly persists. The
UserInterfaceOnly:=Truesetting is not saved with the workbook; it must be reapplied each time the workbook opens.
Tips
- Store the password in a variable at the top of the module so you only change it in one place.
- Use
On Error GoTo Cleanupand put the protect line in the cleanup block so the sheet is always re-protected even if an error occurs. - To protect all sheets in a loop:
For Each ws In Worksheets: ws.Unprotect "pw": ws.Protect "pw": Next ws. - Use
UserInterfaceOnly:=Trueif you want VBA to edit locked cells without unprotecting each time, but reapply it in the Workbook_Open event. - Test on a copy of the workbook first. Protection changes are easy to forget and hard to trace.
- Consider protecting the VBA project itself (Tools > VBAProject Properties > Protection) to keep the password out of sight, but note this is not strong security.
Troubleshooting
If you get run-time error 1004 "Unable to set the Protect property" or the unprotect line fails, the most common cause is a wrong password or a wrong sheet name. Check the tab name exactly, including spaces. If the workbook is shared, unprotect may be blocked; unshare the workbook first (Review > Share Workbook) and try again. If the macro runs but the sheet remains unprotected, an error likely occurred before the protect line; add error handling to see the message. If the Protect method fails with an argument error, you may have passed an unsupported option for your Excel version; remove optional arguments and test with the password only. If the workbook opens with the sheet already unprotected, check whether Workbook_Open code is unprotecting it and failing to re-protect. Finally, if the password prompt appears when you run the macro, the sheet was protected with a password you did not supply; you must use the correct one.
Frequently Asked Questions
Can VBA unprotect a sheet without the password?
No. The Unprotect method requires the correct password if one was set. There is no official bypass; you must supply the password or restore an unprotected copy.
Does Protect with UserInterfaceOnly stay after closing the workbook?
No. UserInterfaceOnly is not saved with the file. Reapply it in the Workbook_Open event each time the workbook opens.
How do I unprotect all sheets at once?
Loop through the Worksheets collection: For Each ws In Worksheets: ws.Unprotect "pw": Next ws. The same loop with Protect re-protects them.
Why does my macro still fail after unprotecting?
The sheet may be protected at the workbook level (Structure) or the workbook may be shared. Check Review > Protect Workbook and Review > Share Workbook. Also confirm you are editing the correct sheet.
Can I protect a sheet without a password?
Yes. Call Protect with no Password argument. Anyone can then unprotect it via the ribbon, so use a password if you need real restriction.
Does sheet protection encrypt my data?
No. Worksheet protection only prevents accidental or casual edits; it is not encryption and can be bypassed by determined users. Use workbook encryption (File > Info > Protect Workbook > Encrypt with Password) for confidentiality.