Excel VBA Project Password
When the high-level code is exposed to the user or reader all they have to do is to copy the code and start using it, so it is better to protect your code with a password.
How to Password Protect the Excel VBA Project?
Like how we password protect our worksheet, workbook similarly we can password protect the VBA codes that we have written.
Follow the below steps to password protect your Excel VBA project.
Step 1: Create a simple macro that needs to be protected.
Sub VBA_Project_Password() Range("A1").Value = "This is a VBA Projet Password Enabler" End Sub
This code will insert “This is a VBA Project Password Enabler” word to the cell A1. Assume you we need to password protect this code.
Step 2: In the visual basic editor window click on the “Tools” tab and chooses “VBA Project Properties”.
Step 3: This will open up the “VBA Project – Project Properties” window which looks like the below one.
Step 4: In this window, we can give a name to the project, we can write a description of the project and we can put any other arguments as well.
At the top of this window, we can see two tabs naming “General” & “Protection”. Choose “Protection”.
Step 5: In this “Protection” we need to enter the password that we are going to use to protect the VBA project. First, check the box “Lock project for viewing”.
Step 6: Now under “Password to view project properties” section enter the password and confirm the password once again then click on “Ok” to close out the above window.
Now our VBA project is password-protected, so in order to view the properties of the project like, module, user forms, and codes written inside the project we need to use the password and see.
Save the workbook, close it and reopen.
Go to Visual Basic Editor and we can see the below window.
Since this project is locked we could see nothing. Click on the PLUS icon on the left-hand side.
Now this will ask you to enter the password to see the properties of the project.
Now we need to enter the password that we have used while protecting the VBA project.
Once the password is entered click on “Ok”, now we can see the properties like worksheet names, modules and their codes.
If the wrong password is entered then we will get the “Invalid Password” message box.
Give Password Input Box to Run the Code
If protecting the VBA project is one thing then asking the user to enter the password to run the password is a different thing.
The below code will ask the user to enter the password to execute the code.
Sub VBA_Project_Password() Dim MyPassword As Variant Dim Password As String Password = 123 MyPassword = Application.InputBox("Enter Your Password", "Password Required to Run the Macro") If MyPassword = Password Then Range("A1").Value = "This is a VBA Project Password Enabler" Else MsgBox "Incorrect Password" End If End Sub
This will ask the user to enter the password when executed.
If the password matches the task will be executed or else we will get the message as below and exit the macro.
Things to Remember
- Without the VBA Project password, it is hard to recover the file, so you need to absolutely sure of what your password is.
- You can use third-party add-ins to break the password.
This has been a guide to VBA Project Password. Here we discuss how to protect your VBA code from others with a password along with practical examples and downloadable excel template. Below you can find some useful excel VBA articles –
- Get Value from One Cell to Another
- Format Date in Excel VBA
- What is ByVal in VBA?
- VBA String to Date
- How to Unprotect Excel Workbook?
- UnProtect Sheet using VBA
- Protect Sheet in VBA
- How to Protect Workbook in Excel?