Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unprotecting a workbook and sheet

49 views
Skip to first unread message

Paul LaPlant

unread,
Jun 12, 2003, 8:34:44 AM6/12/03
to
I know this was stupid, but is there a way to unprotect a password protected
worksheet using VBA. I have a spreadsheet I created a while back and can't
remember the passwords I used to protect the sheet or workbook. There's no
password required to open the spreadsheet, but I cannot edit or unhide my
hidden tabs because the sheets are protected.

Is this a lost cause?

I'm on Excel 2000.

TIA

Paul


Ron de Bruin

unread,
Jun 12, 2003, 10:16:33 AM6/12/03
to
Try this one from Norman Harker and JE McGimpsey

********************************************

The code below has been tested and is designed for easy cut and paste from
this posting.

****

Public Sub AllInternalPasswords()
'Breaks worksheet and workbook structure passwords.
'Bob McCormick probably originator of base code algorithm
'Modified for coverage of workbook structure / windows
'passwords and for multiple passwords
'Norman Harker and JE McGimpsey 27-Dec-2002
'Reveals passwords NOT "the" passwords
Const DBLSPACE As String = vbNewLine & vbNewLine
Dim Mess As String, Header As String
Dim Authors As String, Version As String
Dim RepBack As String, AllClear As String
Dim PWord1 As String
Dim ShTag As Boolean, WinTag As Boolean
Dim w1 As Worksheet, w2 As Worksheet
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim m As Integer, n As Integer, i1 As Integer, i2 As Integer
Dim i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer
Application.ScreenUpdating = False
Header = "AllInternalPasswords User Message"
Authors = DBLSPACE & vbNewLine & "Adapted from Bob " & _
"McCormick base code by Norman Harker " & _
"and JE McGimpsey"
Version = DBLSPACE & "Version 1.1 27-Dec-2002"
RepBack = DBLSPACE & "Please report success or " & _
"failure back to newsgroup."
AllClear = DBLSPACE & "The workbook should now " & _
"be free of all password protection so " & _
"make sure you:" & DBLSPACE & _
"SAVE IT NOW!" & DBLSPACE & _
"and also" & DBLSPACE & _
"BACKUP!, BACKUP!!, BACKUP!!!" & DBLSPACE & _
"Also, remember that the password " & _
"was put there for a reason. Don't " & _
"stuff up crucial formulas or data." & _
DBLSPACE & "Access and use of some data may" & _
"be an offence. If in doubt, don't."
With ActiveWorkbook
WinTag = .ProtectStructure Or .ProtectWindows
End With
ShTag = False
For Each w1 In Worksheets
ShTag = ShTag Or w1.ProtectContents
Next w1
If Not ShTag And Not WinTag Then
Mess = "There were no passwords on sheets, or workbook " & _
"structure or windows." & Authors & Version
MsgBox Mess, vbInformation, Header
Exit Sub
End If
Mess = "After pressing OK button this will take some time." & _
DBLSPACE & "Amount of time depends on how " & _
"many different passwords, the passwords, and" & _
"your computer's specification." & DBLSPACE & _
"Just be patient! Make me a coffee!" & _
Authors & Version
MsgBox Mess, vbInformation, Header
If Not WinTag Then
Mess = "There was no protection to workbook structure " & _
"or windows." & DBLSPACE & _
"Proceeding to unprotect sheets." & _
Authors & Version
MsgBox Mess, vbInformation, Header
Else
On Error Resume Next
Do 'dummy do loop
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
With ActiveWorkbook
.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If .ProtectStructure = False And _
.ProtectWindows = False Then
PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
Mess = "You had a Worksheet Structure or " & _
"Windows Password set." & DBLSPACE & _
"The password found was: " & DBLSPACE & _
PWord1 & DBLSPACE & "Note it down for " & _
"potential future use in other " & _
"workbooks by same person who set this " & _
"password." & DBLSPACE & _
"Now to check and clear other passwords." & _
Authors & Version
MsgBox Mess, vbInformation, Header
Exit Do 'Bypass all for...nexts
End If
End With
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
Loop Until True
On Error GoTo 0
End If
If WinTag And Not ShTag Then
Mess = "Only structure / windows protected with " & _
"the password that was just found." & _
AllClear & Authors & Version & RepBack
MsgBox Mess, vbInformation, Header
Exit Sub
End If
On Error Resume Next
For Each w1 In Worksheets
'Attempt clearance with PWord1
w1.Unprotect PWord1
Next w1
On Error GoTo 0
ShTag = False
For Each w1 In Worksheets
'Checks for all clear ShTag triggered to 1 if not.
ShTag = ShTag Or w1.ProtectContents
Next w1
If Not ShTag Then
Mess = AllClear & Authors & Version & RepBack
MsgBox Mess, vbInformation, Header
Exit Sub
End If
For Each w1 In Worksheets
With w1
If .ProtectContents Then
On Error Resume Next
Do 'Dummy do loop
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If Not .ProtectContents Then
PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
Mess = "You had a Worksheet password set." & _
DBLSPACE & "The password found was: " & _
DBLSPACE & PWord1 & DBLSPACE & _
"Note it down for potential future use " & _
"in other workbooks by same person who " & _
"set this password." & DBLSPACE & _
"Now to check and clear other passwords." & _
Authors & Version
MsgBox Mess, vbInformation, Header
'leverage finding Pword by trying on other sheets
For Each w2 In Worksheets
w2.Unprotect PWord1
Next w2
Exit Do 'Bypass all for...nexts
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
Loop Until True
On Error GoTo 0
End If
End With
Next w1
Mess = AllClear & Authors & Version & RepBack
MsgBox Mess, vbInformation, Header
End Sub

****

** Explanation

The code utilizes the fact that Excel uses a relatively simple algorithm for
sheets and workbook structure and windows. I think it was Bob McCormick who
originally posted a solution that merely involves a 17-18 bit key that can
be found using 11 letter combinations of the letters A and B plus a final
character

The base code was amended and extended. It now removes workbook structure
and workbook window password protection. It also covers the possibility of
more than one password being used. If they are the same, which is frequently
the case, the code takes very little extra time to run because it uses the
first password found. If they are all different and it's a multi-sheet
workbook, then it will take longer; but not as long as you would take to
re-do the entire workbook from scratch!

The code is not intended to be super-efficient and some things in it will
make programmers shudder! But it's all in one module, it's relatively easy
to follow, and it works on all versions of Excel on my model test workbooks.
Code lines have been kept very short so as to facilitate viewing on most
newsreader line length settings and to facilitate copying and pasting to a
VBA Module.

**But how do I use this code?

Simple instructions to those who know how; "Stick in in your Personal.xls or
any other open workbook including the one with the passwords in it, if you
prefer."

But here are instructions aimed at those who don't know anything about VBA
and which explains on a step by step basis how to get this code into their
workbooks:

1. Print out a copy of these instructions (not compulsory but it will make
it
easier)

2. Keep this newsgroup posting open.

3. Open Excel
4. Open the "offending" workbook
5. Press and hold down the Alt key
6. Press F11
7. Let go the Alt key

You're now in the Visual Basic Editor (VBE)
On left of screen is the Project Explorer


8. Select the name of your workbook
9. Right click > Insert > Module

There should now be a blank screen on the right with two drop downs above.

10. Click this post's icon on your task bar
11. Press the maximize button to get it to full screen if it isn't already.
(middle of top right three buttons)

11. Select all of the code above between **** (but not the ****)
12. Edit > Copy (or press copy icon or right click > copy)


13. Activate the Visual Basic Editor screen icon on your task bar.
14. Click in that blank screen area
15. Right click > Paste

16. Click on the Offending File's icon on your task bar (or left-most Excel
icon
in the Visual Basic Editor toolbar)

You should be in your workbook now.

17. Tools > Macro > Macros
18. From the list select:
AllInternalPasswords
19. Click the button marked Run
20. Wait

You'll get various messages whilst it is running.

21. Breathe sighs of relief!


In case you're thinking of trying it, the passwords reported for worksheet
and workbook protection do not work for removing VBA or Excel Workbook file
protection.

One point worth noting is that for any given internal password the same
password as those found by the code will work on other workbooks where
internal sheet and workbook structure / window protection has used the same
passwords. So if you have a stack of these files, it's worth noting down the
password report *exactly* as reported by the code.

If (and when) you have success or failure with the above, please post back
to the newsgroup as it helps others to see whether these things work or not.


HTH

--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl

"Paul LaPlant" <the_la...@hotmail.com> wrote in message news:OAsDC8NM...@TK2MSFTNGP11.phx.gbl...

Alert

unread,
Jun 17, 2003, 6:12:18 AM6/17/03
to
Hi, if you can't be bothered to program your own VBA program to
recover a lost password in Excel, you can download one here:

http://www.intertek.org.uk

"Paul LaPlant" <the_la...@hotmail.com> wrote in message news:<OAsDC8NM...@TK2MSFTNGP11.phx.gbl>...

0 new messages