Sometimes, you may forget the password you have used to protect the VBA project in your Excel file or inherit a workbook from a user with an unknown VBA project password.
This can be frustrating because the password prevents you from modifying, copying, or viewing the VBA project’s code.
In this tutorial, I will show you two simple ways to unlock a VBA project password in Excel.
Method #1: Using a FREE Hex Editor
You can use a hex editor such as HxD, which you can download from the Internet, to unlock a VBA project password in Excel.
A hex editor is an application used to view and modify binary files.
Suppose you have an Excel file with a VBA project with an unknown password and need to view and modify the VBA code.
In this method, I am going to password-protect the VBA project in an Excel file myself first and then copy the hex code for that password.
I will then open an Excel file that has a VBA project that is password protected that I want to unlock, and then replace the hex code of the password used in that file with the hex code of the password that I have created. So in a nutshell I am going to replace password that I don’t know with the one that I know.
Part 1 – Get the HEX Code of a Password We Know
Below are the steps to remove the password protection from a VBA project using the HEX editor:
- Create a new .XLSM workbook.
- Insert a module and enter a sub-procedure.
- Right-click the module and select ‘VBAProject Properties’ on the shortcut menu.
- On the ‘VBAProject – Project Properties’ dialog box, open the ‘Protection’ tab, check the ‘Lock project for viewing’ checkbox, enter a password in the password area, and click OK.
Note: In this example, we have entered the password ‘password.’
- Save and close the .XLSM file.
- Navigate to where the .XLSM file is stored and change its file extension to .ZIP. This step turns the .XLSM file into a zipped file.
Note: If the filename does not show an extension, you can make the extension visible by going to the top of the File Explorer window, then click in the View tab and, checking the ‘File name extensions’ checkbox on the Show/hide group.
- Open the zipped file using WinRAR or any other archiver application.
- Open the XL folder.
- Drag the ‘vbaProject.bin’ file to the desktop to create a copy of the file on the desktop.
- Use a hex editor to open the ‘vbaProject.bin’ file on the desktop.
- Use the Find feature of the hex editor to search for the text string ‘DPB’ in the file.
- Copy the text string in the DPB parameter (inside the double quotes).
In this example, the DPB text string “A0A20C53295329ACD75429C1B38C4B58380324293BC00CAFBCBAAAB665C8E845974A755D00” was generated for the password ‘password’ we entered in step 4 above.
Part 2 – Replace the Hex Code of the Unkown Password With the One We Know
- Open the .XLSM file that has the protected VBA project and change its file extension to .ZIP. This step turns the .XLSM file into a zipped file.
- Open the zipped file using WinRAR or any other archiver application.
- Open the XL folder.
- Drag the ‘vbaProject.bin’ file to the desktop to create a copy of the file on the desktop.
- Use a hex editor to open the ‘vbaProject.bin’ file on the desktop.
- Use the Find feature of the hex editor to search for the text string ‘DPB’ in the file.
- Replace the value in its DPB parameter with the value you copied from the other file in step 12 in Part 1. In our case, I am going to use A0A20C53295329ACD75429C1B38C4B58380324293BC00CAFBCBAAAB665C8E845974A755D00.
Note: If the copied text string is shorter than the one you are replacing, pad it with zeroes until it is the same length as the one you are replacing. However, if it is longer, leave it as is.
- Save the modified ‘vbaProject.bin’ file.
- Replace the ‘vbaProject.bin’ file in the zipped folder with the one you have modified.
- Change the extension of the zipped file to .XLSM. This step converts the zipped file back to an Excel file.
- Open the Excel file and the password to its VBA project is ‘password.’ You can change the password to another password you prefer (or remove the password from the VBA project).
Also read: Unprotect Excel Sheet Without Password
Method #2: Using VBA Code to Unlock VBA Project Password
You can use VBA to unlock a VBA project password in Excel. This method is simpler and better than using a Hex editor.
Note: The VBA code used in this method is credited to a Vietnamese developer nicknamed Siwtom.
Use the steps below:
- Open the Excel file with the VBA project whose password you want to unlock.
- Create a new Excel file and copy the code below into a standard VBA module.
Private Const PAGE_EXECUTE_READWRITE = &H40
Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As LongPtr, Source As LongPtr, ByVal Length As LongPtr)
Private Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As LongPtr, _
ByVal dwSize As LongPtr, ByVal flNewProtect As LongPtr, lpflOldProtect As LongPtr) As LongPtr
Private Declare PtrSafe Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As LongPtr
Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, _
ByVal lpProcName As String) As LongPtr
Private Declare PtrSafe Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" (ByVal hInstance As LongPtr, _
ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, _
ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
Dim HookBytes(0 To 11) As Byte
Dim OriginBytes(0 To 11) As Byte
Dim pFunc As LongPtr
Dim Flag As Boolean
Private Function GetPtr(ByVal Value As LongPtr) As LongPtr
GetPtr = Value
End Function
Public Sub RecoverBytes()
If Flag Then MoveMemory ByVal pFunc, ByVal VarPtr(OriginBytes(0)), 12
End Sub
Public Function Hook() As Boolean
Dim TmpBytes(0 To 11) As Byte
Dim p As LongPtr, osi As Byte
Dim OriginProtect As LongPtr
Hook = False
#If Win64 Then
osi = 1
#Else
osi = 0
#End If
pFunc = GetProcAddress(GetModuleHandleA("user32.dll"), "DialogBoxParamA")
If VirtualProtect(ByVal pFunc, 12, PAGE_EXECUTE_READWRITE, OriginProtect) <> 0 Then
MoveMemory ByVal VarPtr(TmpBytes(0)), ByVal pFunc, osi + 1
If TmpBytes(osi) <> &HB8 Then
MoveMemory ByVal VarPtr(OriginBytes(0)), ByVal pFunc, 12
p = GetPtr(AddressOf MyDialogBoxParam)
If osi Then HookBytes(0) = &H48
HookBytes(osi) = &HB8
osi = osi + 1
MoveMemory ByVal VarPtr(HookBytes(osi)), ByVal VarPtr(p), 4 * osi
HookBytes(osi + 4 * osi) = &HFF
HookBytes(osi + 4 * osi + 1) = &HE0
MoveMemory ByVal pFunc, ByVal VarPtr(HookBytes(0)), 12
Flag = True
Hook = True
End If
End If
End Function
Private Function MyDialogBoxParam(ByVal hInstance As LongPtr, _
ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, _
ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
If pTemplateName = 4070 Then
MyDialogBoxParam = 1
Else
RecoverBytes
MyDialogBoxParam = DialogBoxParam(hInstance, pTemplateName, _
hWndParent, lpDialogFunc, dwInitParam)
Hook
End If
End Function
Sub VBAunprotected()
If Hook Then
MsgBox "VBA Project is unprotected!", vbInformation, "*****"
End If
End Sub
- Execute the ‘VBAUnprotected’ subroutine at the bottom of the code.
The above steps remove the VBA project password from the target workbook. You can protect the VBA project again with a password you can remember.
In this tutorial, I have shown you two ways to unlock a VBA project password in Excel. I hope you found the tutorial helpful.
Other Excel articles you may also like:
- How to Lock Cells in Excel [Mac, Windows]
- How to Remove Password From Excel?
- Delete Files in a Folder Using VBA in Excel
- Lock Cells in Excel (Shortcut)
- Protect Excel Workbook Using VBA
- How to Make Excel File Read Only
- How to Open Excel File [xls, xlsx] Online (for FREE)
- How to Lock a Pivot Table in Excel
- Unblock Macros in Excel Files Downloaded From the Web
Hi Steve, great stuff you have here. I was also using Excel extensively in my former job. Now I am retired, but use excel en macro’s still. This I use it now to beat the stockmarket. Have you ever thought to do that? Best regards, Luc
Nice save.. 🙂 Clear, easy to follow and it works. Will be keeping an eye on this site..
Thanks!
Wes J