Password-Protect and Unprotect All Excel Worksheets in One Fell SwoopSadly, there is no standard feature in Excel that will enable you to protect and unprotect all worksheets in one go; however, some simple code can make it happen. Excel provides protection that you can add to an Excel worksheet by selecting Tools Sometimes, though, you want to password-protect and unprotect all worksheets in a workbook in one step because protecting and unprotecting each worksheet individually is a huge nuisance. Here is how you can simplify this task. Open the workbook to which you want to apply the code. Or, select Window Next, select Tools From the toolbox, select a TextBox (indicated as ab|). Click onto the UserForm to add the TextBox to the UserForm. Position it in the top left of your form and size it to your preference. Ensure that the textbox is still selected and then select View With the CommandButton still selected, select View Now select the UserForm and, from its Properties window, find Caption and change it to Protect/Unprotect all sheets. Your form should look like that shown in Figure 7-7. Figure 7-7. UserForm inserted in the VBE
Select View
Private Sub CommandButton1_Click( )
Dim wSheet As Worksheet
For Each wSheet In Worksheets
If wSheet.ProtectContents = True Then
wSheet.Unprotect Password:=TextBox1.Text
Else
wSheet.Protect Password:=TextBox1.Text
End If
Next wSheet
Unload me
End Sub
The code loops through all worksheets in the active workbook. If one is protected, it unprotects it using the password entered into the text box. If the worksheet is already unprotected, it protects it using the password entered into the text box. Now select Insert
Sub ShowPass( )
UserForm1.Show
End Sub
This is used to launch the UserForm. Close the window to get back to Excel. Select Tools
If you're protecting the contents only from yourself, the following macro lets you perform the same tasks with a blank password instead:
Option Explicit
Sub Protect_Unprotect( )
Dim wSheet As Worksheet
For Each wSheet In Worksheets
With wSheet
If .ProtectContents = True Then
.Unprotect Password:=""
Else
.Protect Password:=""
End If
End With
Next wSheet
End Sub
Although it's not very secure, it's definitely convenient. |
