Protect VBA code with password

This Excel VBA tutorial explains how to protect VBA code with password.

You may also want to read:

Excel VBA protect worksheet with password

Excel VBA hide worksheet with password (xlVeryHidden)

Excel VBA Add password to Excel workbook in folder

Protect VBA code with password

When you write Macro / VBA code in Excel, other people can easily see the source code by clicking ALT+F11 andĀ  then copy / modify your code. If you do not want anyone to see the VBA code (to steal your hard work), you can set a password to protect the VBA code, they will still be able to run the Macro without seeing the VBA code. The trick I demonstrate below is also applicable to other Office products that allows you to write VBA (Microsoft Word, Microsoft Access, etc), the naming in the menu could be slightly different though.

Assume that we have written a sub procedure called test() inĀ Module1 of workbook Book1 .

Protect VBA code with password 01

 

Navigate to Tools > VBAProject Properties

Protect VBA code with password 02

 

Navigate to Protection tab > check the box Lock project for viewing, then enter the password and confirm password > OK

Note that if you don’t check the box Lock project for viewing, the password only protects the configuration in VBAProject Properties, but not protecting the VBA code.

Protect VBA code with password 04

 

Now when you close the VBE and come back again, your will not be asked to enter password to view the VBA code, until you save and close the workbook, then reopen.

Double click on VBAProject (Book1.xlsm), a prompt box pops up asking you for password, that means the password is protected for the whole workbook, not only at specific Module.

Protect VBA code with password 05

After you have successfully unprotected the code, even if you close VBE, you will not be asked again for password, until you close the workbook and come back in.

 

Wyman W
Wyman is a Business Analyst based in Hong Kong, specialized in creating custom Function and Sub solutions, and is proficient in report automation with Access.

He is also a:
- Microsoft Most Valuable Professional (Excel)
- Microsoft Community Contributor
- Microsoft Office Specialist
- Microsoft Specialist
- Microsoft Technical Associate
- Microsoft Certified Professional
- IBM SPSS Specialist

Leave a Reply

Your email address will not be published. Required fields are marked *