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 .
Navigate to Tools > VBAProject Properties
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.
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.
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.