Unprotect Sheets - Multiple Passwords - VBA

spydey

Active Member
Joined
Sep 19, 2017
Messages
293
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!
 

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,807
Code:
on error resume next
For Each ws In ActiveWorkbook.Worksheets
   ws.Unprotect password:="Pass"
   ws.Unprotect password:="1Test"  
Next
 

spydey

Active Member
Joined
Sep 19, 2017
Messages
293
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
 

Forum statistics

Threads
1,081,863
Messages
5,361,743
Members
400,654
Latest member
Pinaki Chatterjee

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top