Excel VBA Project Password
If a developer wants to hide the code of the project from the other users, VBA has provided us with a tool to do so, now using this tool we can password protect a single project or multiple projects when we right-click on a project we get an option for VBA project properties and in the protection segment we can password protect the project.
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 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 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 we need to password protect this code.
Step 2: In the visual basic editor window, click on the “Tools” tab, and chooses “VBAProject Properties.”
Step 3: This will open up the “VBAProject – 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.
4.9 (1,353 ratings) 35+ Courses | 120+ Hours | Full Lifetime Access | Certificate of Completion
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 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 project is password-protected, so in order to view the properties of the project like a 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 project.
Once the password is entered, click on “Ok” now, we can see the properties like worksheet names, modules, and 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 a Project password, it is hard to recover the document in excel, 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 Excel VBA Project Password. Here we discuss how to protect our VBA code from others with a password along with practical examples and a downloadable excel template. Below you can find some useful excel VBA articles –