Excel VBA Unprotect Sheet
There are situations where we protect our worksheets to prevent it from the user from manipulating the worksheets. While protecting the worksheet we usually maintain the same password. In this article, we will show you the way to unprotect the sheet using VBA coding.
Unprotecting the sheet is 99% simpler than protecting them, we just need the password to unprotect the worksheet.
The very first thing we need to do is to mention the worksheet name we are going to unprotect. For example, if you are looking to unprotect the sheet named as “Sales Data” then your VBA code should be like this.
Then access the Unprotect method.
As we can see in the above image we don’t see any kind of IntelliSense list to assist us. This makes the job tougher, especially for the new learner.
To overcome this I usually rely on declaring a VBA variable as “Worksheet”.
Sub Unpretect_Example1() Dim Ws As Worksheet End Sub
Now set the declared variable to the respective sheet.
Sub Unpretect_Example1() Dim Ws As Worksheet Set Ws = Worksheets("Sales Data") End Sub
Now use a variable to access to all the properties and methods of the declared variable.
As you can see in the above image we can access all the properties and methods. Select the “Unprotect” method from the IntelliSense list.
It is asking the password to unprotect the Sheet. I have set the password as “Excel@1234” so I will supply the password as same.
This will unprotect the worksheet named as “Sales Data”.
Sub Unpretect_Example1() Dim Ws As Worksheet Set Ws = Worksheets("Sales Data") Ws.Unprotect Password:="Excel@1234" End Sub
If there is no password then we just need to use the method “Unprotect” and ignore the “Password” parameter.
Sub Unpretect_Example1() Dim Ws As Worksheet Set Ws = Worksheets("Sales Data") 'Change the worksheet name as per your requirement Ws.Unprotect 'If there is no password just pass "UnProtect" method End Sub
If there is any password then we need to enter the password in double-quotes. Password is case sensitive so remember them carefully.
Example #2 – Unprotect All Excel Sheets with Just a Click
We have seen how to unprotect the particular worksheet. Imagine you have many worksheets and all the worksheets are protected with the same password, then we cannot keep writing codes for each worksheet separately.
In these cases, we need to use loops to loop through the collection of worksheet object and unprotect them with ease.
Below code will loop through all the worksheets and unprotect the sheet.
Sub Unpretect_Example2() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets Ws.Unprotect Password:="Excel@1234" 'Change the password as you have mentioned while protecting them. Next Ws End Sub
Example #3 – Special Situations
Situation 1: Have ever imagined when the supplied password is wrong. When the supplied password is wrong we will get 1004: Run Time Error.
To handle these errors we can use “On Error GoTo Label” option. Below code is the example of the same.
Sub Unpretect_Example3() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets On Error GoTo Errormessage Ws.Unprotect Password:="Excel@1234" 'Change the password as you have mentioned while protecting them. Errormessage: MsgBox "Wrong Password" Next Ws End Sub
The above code will show a nice message box saying “Wrong Password”.
Situation 2: When the sheet is protected without a password and if you supply a random password it will still unprotect the worksheet without showing any kind of errors.
Situation 3: When the sheet is protected by a password but if you don’t supply any password then VBA will pop up as a password input box to enter the password.
In the above input box, we need to enter our password to unprotect the worksheet. If you click on the Cancel button it will exit the VBA subprocedure without showing anything unless if there is any user message box.
This has been a guide to VBA UnProtect Sheet. Here we learn how to use vba code to unprotect excel sheet with password along with some simple to advanced examples. Below are some useful excel articles related to VBA –