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 codingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task..
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 “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 variableDeclaring A VBA VariableVariable declaration is necessary in VBA to define a variable for a specific data type so that it can hold values; any variable that is not defined in VBA cannot hold values. 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 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 “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. The 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.
The 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 the 1004: Run Time Error.
To handle these errors, we can use the “On Error GoTo Label” option. The below code is an 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, “The 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 subprocedureVBA SubprocedureSUB in VBA is a procedure which contains all the code which automatically gives the statement of end sub and the middle portion is used for coding. Sub statement can be both public and private and the name of the subprocedure is mandatory in VBA. 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 the excel sheet with password along with some simple to advanced examples. Below are some useful excel articles related to VBA –