Unprotect Sheets - Multiple Passwords - VBA

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
So I have a workbook with 100+ sheets. The sheets are protected and use either one of two different passwords.

I need a way to unprotect all of the sheets cycling through the two different passwords if one or the other fail to unprotect the worksheet.

I don't know which sheets are using which password.

The Passwords are: Test1 & 1Test

I have been using the following code in previous projects to unprotect worksheets, but in those cases there was only one password being used.

Code:
[COLOR=#333333]Sub UnProtectSheets()[/COLOR]

[COLOR=#333333]Dim ws As Worksheet[/COLOR]

[COLOR=#333333]For Each ws In ActiveWorkbook.Worksheets[/COLOR]
[COLOR=#333333]ws.Unprotect password:="Test1"[/COLOR]
[COLOR=#333333]Next ws[/COLOR]

[COLOR=#333333]End Sub[/COLOR]


The issue I have is that when I try to use the above, if the password is anything other than Test1, it fails, for obvious reasons.

How can I unprotect a worksheet, and if Test1 fails to unprotect it, then have it unprotect with 1Test??

Something like this:

Code:
[COLOR=#333333]Public Sub DeProtectAll()[/COLOR]

[COLOR=#333333]Dim ws As Worksheet[/COLOR]

[COLOR=#333333]For Each ws In ActiveWorkbook.Worksheets[/COLOR]
[COLOR=#333333]If ws.Unprotect password:="Pass" Fails Then
[/COLOR]   ws.Unprotect password:="1Test"
End If
[COLOR=#333333]Next ws[/COLOR]

[COLOR=#333333]End Sub

[/COLOR]

Granted, I know that the above isn't really how the code should be written, but at least it gives an idea of what I am looking for.

Any thoughts, ideas, suggestions?

Thanks in advance!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
on error resume next
For Each ws In ActiveWorkbook.Worksheets
   ws.Unprotect password:="Pass"
   ws.Unprotect password:="1Test"  
Next
 
Upvote 0
Code:
on error resume next
For Each ws In ActiveWorkbook.Worksheets
   ws.Unprotect password:="Pass"
   ws.Unprotect password:="1Test"  
Next


Genius!! Thank you sir for your time and assistance.


-Spydey
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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