Excel VBA Unprotect Worksheet: Step-by-Step Guide
In this article
To unprotect a worksheet in Excel VBA, use the Unprotect method on the worksheet object, optionally supplying the password. The simplest working line is ActiveSheet.Unprotect "yourpassword" — or just ActiveSheet.Unprotect if no password was set. This lets your macro edit locked cells, change formatting, or delete rows without the protection dialog blocking it. You will need the Developer tab enabled and a basic understanding of where to paste code, but no programming experience is required.
What You'll Need
- Excel desktop (Windows or Mac) with macros enabled. The VBA editor is not available in Excel for the web.
- The Developer tab visible on the ribbon. If it is missing, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the right-hand list. Labels can differ slightly by Excel version and platform.
- The password for the protected sheet, if one was set. Without it, the Unprotect method fails.
- A workbook saved as .xlsm (macro-enabled) if you plan to keep the code.
Step-by-Step
- Open the workbook that contains the protected sheet and press
Alt+F11(Windows) orFn+Option+F11(Mac) to open the Visual Basic Editor. - Insert a module. In the VBE menu, choose Insert > Module. A blank code window appears. Keeping code in a standard module makes it easy to run manually and to reuse.
- Write the unprotect line. Type the following, replacing Sheet1 with your sheet's code name and yourpassword with the actual password:
If the sheet has no password, omit the Password argument:Sub UnprotectSheet() Worksheets("Sheet1").Unprotect Password:="yourpassword" End SubWorksheets("Sheet1").Unprotect. - Run the macro. Place the cursor inside the Sub and press
F5, or go back to Excel and use Developer > Macros, select UnprotectSheet, and click Run. - Verify. Return to the sheet and try editing a previously locked cell. If the edit is accepted, protection is off. You can also check the Review tab — the Unprotect Sheet button will be greyed out or the sheet will no longer show as protected.
- Re-protect when done (recommended). Add a matching line at the end of your macro so the sheet does not stay open to edits:
This keeps the original protection intact after your code finishes.Worksheets("Sheet1").Protect Password:="yourpassword"
Common Mistakes
- Wrong sheet reference.
Worksheets("Sheet1")refers to the tab name, not the code name shown in the Project Explorer. If the tab is named "Data", useWorksheets("Data")or the code nameSheet1.Unprotect. - Password typos and case. VBA passwords are case-sensitive. A trailing space or a smart quote pasted from a document will cause a runtime error.
- Forgetting to re-protect. Leaving the sheet unprotected after the macro runs defeats the purpose of protection and can let other users change formulas.
- Assuming Unprotect removes workbook-level protection. Worksheet protection and workbook structure protection are separate. Use
ThisWorkbook.Unprotectfor the workbook, not the sheet. - Running code on the wrong workbook. If more than one workbook is open, an unqualified
ActiveSheetmay target the wrong file. Qualify with the workbook name.
Tips
- Store the password in a cell or a named constant rather than hard-coding it in many places, so a change only needs one edit.
- Use
On Error Resume Nextsparingly around Unprotect only if you want the macro to continue when the sheet is already unprotected; otherwise handle the error explicitly. - If you only need to write to a few locked cells, consider unlocking those cells before protecting the sheet (Format Cells > Protection tab) instead of unprotecting the whole sheet.
- Keep a backup copy of the workbook before running macros that change protection, especially on shared files.
- For sheets protected with "UserInterfaceOnly", you can set that option in the Protect method so VBA can edit cells while the sheet stays protected for users.
Troubleshooting
If the macro stops with a "password is not correct" or "sheet is protected" error, first confirm the exact password and that the sheet is actually protected. Check the Review tab: if Unprotect Sheet is available, the sheet is protected; if not, it is already unprotected and the error is coming from elsewhere. Next, verify the sheet name in your code matches the tab exactly, including spaces. If the macro runs but cells remain locked, the protection may be at workbook level or the cells themselves may be locked with a different setting — check Format Cells > Protection. If you see a "method failed" error, the workbook may be shared or read-only; close other users' copies or save a local copy first. On Mac, some older Excel versions handle the Password argument differently; if the line fails, try Unprotect ("yourpassword") with parentheses. Finally, if the VBA project itself is password-protected, you cannot view or edit the code without that password — that is a separate lock from worksheet protection.
Frequently Asked Questions
Can I unprotect a sheet without knowing the password?
Not through the normal Unprotect method. Excel's worksheet protection is not strong encryption, but bypassing it is outside the scope of this guide and may violate policy on files you do not own. If you own the file and forgot the password, restore from a backup or rebuild the sheet.
Does Unprotect work on all protected sheets at once?
No. You must call Unprotect on each worksheet individually, for example by looping through Worksheets and calling Unprotect on each one.
Will unprotecting a sheet remove data validation or formulas?
No. Unprotect only removes the editing restrictions. Formulas, validation rules and formatting stay as they are unless your macro changes them.
Why does my macro still fail after Unprotect?
The most common cause is that the code is trying to edit a different sheet or a protected workbook structure. Check the sheet reference and whether the workbook itself is protected under Review > Protect Workbook.
Can I unprotect a sheet in Excel for the web?
No. The VBA editor and macros are not available in Excel for the web. You need the desktop app to run VBA code.
Is it safe to leave the password in the code?
Anyone who can open the VBA editor can read it. If the workbook is shared, protect the VBA project with a password or avoid storing the password in plain text.