Let me preface this article by saying that this will not help you recover lost data, or gain access to protected data that you otherwise wouldn’t have access to. What it will do is allow you to unlock a password protected worksheet in Excel, so that you can edit it as necessary. Simply put, if you don’t already have access to the worksheet, this macro will not help you.
Now that the disclaimer is out of the way, let’s figure out why people password protect worksheets to begin with. By protecting a worksheet and the contents of locked cells, users can keep themselves or other users from:
- accidentally (or intentionally) removing formulas, or other contents of locked cells
- adding or deleting rows and columns
- changing cell, column or row formats
- sorting data
- using AutoFilter or PivotTable reports
- editing objects or scenarios
Protecting a worksheet will not keep users from
- editing any unlocked cells in the worksheet
- viewing all data in the worksheet, regardless of if it is in a locked cell or not
Being able to unlock a password protected worksheet is useful, when
- you have forgotten the password on your own worksheet
- a co-worker, or other user has password protected a worksheet that you now need to edit, and they are not available to unlock the sheet for you
- you have a need to perform an analysis of the data in a password protected worksheet, but are unable to do so due to the locked cells
- you would like to sort/filter the data in a password protected worksheet, or create a PivotTable report from the data
Since Microsoft Excel is not a “secure” program, it is very easy to unlock the password of a password protected worksheet. This is because there are several different combinations of passwords that Excel will accept to unlock the worksheet. For example, a worksheet with the password “treehouse”, can also be unlocked with the password “AAAABAABBBB/”. The opposite is also true (i.e. protecting with the password “AAAABAABBBB/” can be unlocked with the password “treehouse”). Go ahead; try locking a worksheet with the password treehouse, and use AAAABAABBBB/ to unlock it.
The macro code below will “unlock” one worksheet at a time, using the method used above. It won’t provide you with the actual password someone typed in, but rather a random sequence of letters or symbols that will work to unlock the worksheet.
Sub PasswordRecovery()
'
'MACROS BY EXCELZOOM.COM
Dim i As Integer, j As Integer, k As Integer
Dim l As Integer, m As Integer, n As Integer
Dim i1 As Integer, i2 As Integer, i3 As Integer
Dim i4 As Integer, i5 As Integer, i6 As Integer
On Error Resume Next
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
ActiveSheet.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 ActiveSheet.ProtectContents = False Then
MsgBox "One usable password is " & Chr(i) & Chr(j) & _
Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
Exit Sub
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
End Sub