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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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,214,541
Messages
6,120,110
Members
448,945
Latest member
Vmanchoppy

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