This protection also helps maintain data consistency and reduce the likelihood of errors.
Here is an example VBA code that you can use to protect all worksheets in a workbook with a password:
Sub ProtectAllSheets()
Dim ws As Worksheet
Dim password As String
password = “Test123”
For Each ws In Worksheets
ws.Protect Password:=password
Next ws
End Sub
Code Explanation:
- Dim password As String: Declares a password variable to store the password to be used.
- password = “Test123”: Sets the default value for the password. You can replace “Test123” with the password you want.
- ws.Protect Password:=password: Sets the Protect property of the worksheet with a predefined password, so that the worksheet will be protected.
How to Use Macros to Unprotect All Worksheets at Once
Unprotecting an Excel worksheet is indispensable when you want to change or update previously locked data. If many worksheets are password protected, opening them one by one can be very time-consuming.
Using VBA macros, you can quickly unprotect all worksheets in a workbook, making it easy to access and edit data.
Here is an example VBA code that you can use to unprotect all worksheets in a workbook:
Sub UnprotectAllSheets()
Dim ws As Worksheet
Dim password As String
password = “Test123”
For Each ws In Worksheets
ws.Unprotect Password:=password
Next ws
End Sub
Code Explanation:
- password = “Test123”: Sets the default value for the password. You must replace “Test123” with the appropriate password.
- ws.Unprotect Password:=password: Uses the specified password to open the worksheet protection.