Excel VBA Unlock Worksheet: Step-by-Step Guide
In this article
You can unlock a worksheet from VBA with a single line: ActiveSheet.Unprotect "yourpassword". If the sheet has no password, use ActiveSheet.Unprotect with no argument. This works only when you know the password (or the sheet was protected without one); VBA cannot bypass a password you do not know. The steps below cover the safest way to do it, plus how to re-protect the sheet afterwards.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- The password for the protected worksheet, if one was set.
- A copy of the workbook to test on, so a mistake does not affect your original file.
Step-by-Step
- Open the workbook and press
Alt + F11(Windows) orOption + F11(Mac) to open the Visual Basic Editor. - In the Project Explorer, find the workbook and the worksheet you want to unlock. If the Project Explorer is hidden, press
Ctrl + R. - Insert a new module: Insert > Module. This keeps your code separate from the sheet's own code.
- Type the following procedure, replacing
Sheet1with your sheet's name andyourpasswordwith the real password:
If the sheet has no password, useSub UnlockSheet() Worksheets("Sheet1").Unprotect Password:="yourpassword" End SubWorksheets("Sheet1").Unprotect. - Press
F5to run the macro. If the password is correct, the sheet is now unprotected. - To re-protect the sheet after your changes, add a second line before
End Sub:Worksheets("Sheet1").Protect Password:="yourpassword". Run the macro again. - Close the Visual Basic Editor and save the workbook as a macro-enabled file (.xlsm) if you want to keep the code.
Menu labels and the exact location of the Developer tab can differ slightly by Excel version, device and region.
Common Mistakes
- Wrong sheet name.
Worksheets("Sheet1")must match the tab name exactly, including spaces. Use the name shown on the tab, not the code name in the Properties window. - Wrong password or extra spaces. Passwords are case-sensitive. Copy the password rather than retyping it.
- Forgetting to re-protect. If you leave the sheet unprotected, other users can edit locked cells. Add the
Protectline back. - Running on the wrong workbook.
ActiveSheetrefers to whatever sheet is visible. Naming the sheet explicitly is safer. - Saving as .xlsx. Macros are stripped from .xlsx files. Save as .xlsm.
Tips
- Store the password in a cell on a hidden sheet or ask for it with an
InputBoxinstead of hard-coding it in the module. - Use
ThisWorkbook.Worksheets("Sheet1")to make sure the macro always targets the workbook that contains it. - Test on a copy first. Unprotecting a sheet does not change data, but a mistaken
Protectcall can lock cells you did not intend to lock. - If you only need to unprotect once, you can also do it manually: Review > Unprotect Sheet, then enter the password.
Troubleshooting
- Run-time error 1004: usually a wrong sheet name or a wrong password. Check the tab name and try the password again.
- Nothing happens: make sure the macro is in a standard module, not in the sheet's code window, and that macros are enabled (File > Options > Trust Center > Macro Settings).
- Password rejected: confirm Caps Lock is off and that you are using the worksheet password, not the workbook password. Workbook-level protection uses
ThisWorkbook.Unprotectinstead. - Sheet still locked after running: another macro or an event may be re-protecting it. Check for a
Worksheet_Changeevent in the sheet's code. - If that did not work: close and reopen Excel, then try again on a fresh copy of the file. If the password is genuinely lost, VBA cannot recover it; you would need to rebuild the sheet or restore from a backup.
Frequently Asked Questions
Can VBA unlock a worksheet without the password?
No. Unprotect requires the correct password if one was set. There is no supported VBA method to bypass it.
Does unprotecting a sheet delete data?
No. It only removes the edit restrictions. Your cell contents, formulas and formatting stay the same.
What is the difference between worksheet and workbook protection?
Worksheet protection locks cells on one sheet. Workbook protection locks the structure (adding, deleting or moving sheets). Use ThisWorkbook.Unprotect for the latter.
Why does my macro fail with error 1004?
The most common causes are a misspelled sheet name or an incorrect password. Verify both and run the macro again.
Can I unlock all sheets at once?
Yes. Loop through the collection: For Each ws In Worksheets: ws.Unprotect "password": Next ws. This assumes every sheet uses the same password.
Do I need to save as .xlsm?
Only if you want to keep the macro. If you run it once and delete the code, a normal .xlsx file is fine.