VBA - Unprotect multiple sheets at once

Peter Muller

Board Regular
Joined
Oct 15, 2018
Messages
133
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I have a workbook that I share with other users. Several sheets are locked, and password protected.

I am looking for code to unprotect all the locked sheets, which is password protected, when I work in the file, but instead of entering the unprotect password several times, can I just enter the password once?

Using code:

Sub UnprotectAll()

Dim S As Variant
For Each S In Array("Report", "Analysis", "Budget")
Worksheets(S).Unprotect
Next S

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Do you want the macro to ask you for the password, or do you want it hardcoded?
 
Upvote 0
Do you want the macro to ask you for the password, or do you want it hardcoded?
It must ask for the password so that the other users cannot unlock it, but at the moment I have to enter the password for each protected sheet, wanting to only type it in once if possible
 
Upvote 0
Ok, how about
VBA Code:
Sub PeterMuller()
   Dim Ws As Worksheet
   Dim Pword As String
   
   Pword = InputBox("Please enter the password")
   If Pword = "" Then Exit Sub
   
   For Each Ws In Sheets(Array("Report", "Analysis", "Budget"))
      On Error GoTo Oops
      Ws.Unprotect Pword
   Next Ws
   Exit Sub
Oops:
   MsgBox "The password used is incorrect"
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Sub PeterMuller()
   Dim Ws As Worksheet
   Dim Pword As String
  
   Pword = InputBox("Please enter the password")
   If Pword = "" Then Exit Sub
  
   For Each Ws In Sheets(Array("Report", "Analysis", "Budget"))
      On Error GoTo Oops
      Ws.Unprotect Pword
   Next Ws
   Exit Sub
Oops:
   MsgBox "The password used is incorrect"
End Sub
Perfect, thank you so much :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,693
Members
449,331
Latest member
smckenzie2016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top