top of page

VBA CODE: Password Protect and Unprotect Sheets | Microsoft Excel

If you have too many worksheets in an excel workbook and want to password protect/unprotect all your worksheets in one go, you can use the below VBA code to do the same.


VBA Code: Protect Sheets

The below code will require you to specify the password within the VBA code and the same password will be used to unprotect the sheets.

Sub ProtectAllSheets()
Dim ws As Worksheet
Dim password As String
password = "password123"
For Each ws In Worksheets
   ws.Protect password:=password
Next ws
End Sub

VBA Code: Unprotect Sheets

The below code will require you to mention the same password used while protecting the sheets to unprotect the same.

Sub UnProtectAllSheets()
Dim ws As Worksheet
Dim password As String
password = "password123"
For Each ws In Worksheets
    ws.Unprotect password:=password
Next ws
End Sub


Important Considerations:

  • Unprotect Password should be the same as that been used while protecting the sheets. An error message will appear if it's not.

  • Protect and Unprotect password "password123" can be replaced as per user requirement.

 

Disclaimer: We, in our example, only cover how to perform such functions in excel and less focusing on the type of example we take. Our motive is to deliver the exact solution to the queries on "How To" in the most simplest way. Of course, the application of these function can be seen in our advanced modules with more complex examples and datasets.

 

Feel free to share your views and ask for models in the #comment section. If you like our content, please hit a #like button.

46 views0 comments
bottom of page