VBA Tip: Protect VBA Code with a Password

You have the option to protect access to your VBA project with a password.

Without this password, it will not be possible* to access the VBA code, UserForms, etc.

* You will find out at the bottom of the page whether this protection is truly effective.

Add a VBA Password

To add a password, right-click on the VBA project of the workbook in the editor and select "VBAProject Properties":

excel vba password

Click on the "Protection" tab, check the box, enter the password, and confirm:

excel vba add password

To verify if it worked, enter at least one line of VBA code, save the workbook in the xlsm format, and close the workbook.

Upon reopening, the password will be required to access the VBA project:

excel vba password access request

Effectiveness of the Protection

This protection is sufficient to prevent an average user from accessing the VBA project.

However, be aware that there are techniques that allow knowledgeable users to access the VBA code despite the password.

Therefore, understand that your VBA code is not completely protected, but for many use cases (such as preventing colleagues from tampering with your application's code), this protection will be sufficient.

How to Protect the Code More Effectively?

If your VBA code is valuable and deserves stronger protection, you can obfuscate your code (in addition to using a VBA password).

Obfuscation involves making the VBA code unreadable (while still being functional).

Here is an example of obfuscated VBA code:

Function b8df51c05627c2c7a6ccb9687a61aa3db(ByVal m02bfea39d5c03072df2b9ac69a63d02c)
If bfec0e4a3d4ed8a6e788ae883bb07b4aa = 1 Then b31f894d874702cfe0f293382dc730879
b8df51c05627c2c7a6ccb9687a61aa3db = ""
bd2616cf6752c53420877818657e28354 = UBound(b3fe457c44d7d5c35ee73713e6c24a249)
For n8f48522fdf95bb09a3b85aec5bdc8664 = 0 To bd2616cf6752c53420877818657e28354
If b3fe457c44d7d5c35ee73713e6c24a249(n8f48522fdf95bb09a3b85aec5bdc8664, 0) = m02bfea39d5c03072df2b9ac69a63d02c Then
If b3fe457c44d7d5c35ee73713e6c24a249(n8f48522fdf95bb09a3b85aec5bdc8664, 1) = "" Or b3fe457c44d7d5c35ee73713e6c24a249(n8f48522fdf95bb09a3b85aec5bdc8664, 2) = "" Then
b8df51c05627c2c7a6ccb9687a61aa3db = b3fe457c44d7d5c35ee73713e6c24a249(n8f48522fdf95bb09a3b85aec5bdc8664, 1) & b3fe457c44d7d5c35ee73713e6c24a249(n8f48522fdf95bb09a3b85aec5bdc8664, 2)
b8df51c05627c2c7a6ccb9687a61aa3db = b3fe457c44d7d5c35ee73713e6c24a249(n8f48522fdf95bb09a3b85aec5bdc8664, 1) & " - " & b3fe457c44d7d5c35ee73713e6c24a249(n8f48522fdf95bb09a3b85aec5bdc8664, 2)
End If
Exit For
End If
End Function

For more information on this process or to use the obfuscation utility provided by Excel-Pratique, click here: obfuscate VBA code