Excel VBA Project Password
Excel VBA Project Password is a kind of tool in VBA which is used to protect the project code using the password. By this, we can protect the worksheet and the projects as well. If we protect the project, in another way we are also protecting the Excel workbook as well. We can either protect or preserve one project in VBA or multiple projects. And this is can be using the same procedure for both categories.
Usually, most of the code written in VBA has or requires only a project in which all the worksheets, modules, user forms can be seen.
How to Use VBA Project Password in VBA?
To protect the VBA Project using Password or to enable the password set in a VBA Project, we will be using the VBA Project properties option which is available in the Tools menu bar in VBA as shown in the below steps. Then we will be getting the VBA Project properties window where we can set the password whichever we want, to protect the currently available projects in VBA.
To apply a password in the VBA Project, first, we need to have a code in a module. This could be any simple code as per our choice or requirement. For this, follow the below steps:
Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.
Step 2: Now write the subprocedure for the VBA Project password.
Sub VBA_ProjectPW() End Sub
Step 3: Use DIM to define a variable as an Integer.
Sub VBA_ProjectPW() Dim A As Integer End Sub
Step 4: And last to close the code, use Debug print to print the value whichever could be stored in variable A. This is the simplest code that we protect using the VBA Project password.
Sub VBA_ProjectPW() Dim A As Integer Debug.Print A End Sub
Step 5: To test this, we would run this code by pressing F5 or Play Button is mentioned below the menu bar.
We would notice that the code has run without any error. Now to protect the VBA Project with a password, go to Tools menu option and select VBA Project properties as shown below.
Step 6: We will then get the Project Properties as shown below. And in that go to the Protection tab.
Step 7: Now in the Protection tab’s Password and Confirm password section write the password which you want. Preferably the password should be strong enough not be cracked easily by anyone.
Step 8: We have chosen the password as “Pa$$w0rd123” in the combination of Upper case, lower case, special character, and numbers. The password should be the same in Password and Confirm Password section as shown below. It is better to copy and paste the password to match and then Click on OK to exit.
Step 9: Now save the code and then close the VBA and Excel files. To check this, reopen the file. In the VBA window, we will see all the options like Modules in the VBA Project is hidden. To test, click on the plus sign of the VBA Project.
Step 10: We will get the password window there. To enter the password which we had chosen. And then click on Ok.
Step 11: This would enable the VBA Project with all the options and we will be able to see the code written in the module.
Step 12: And if we enter the incorrect password we would then get an incorrect message alert.
There is another way to protect the VBA Project using passwords with the help of code. For this, follow the below steps:
Step 1: In the same module and write the subprocedure.
Sub VBA_ProjectPW2() End Sub
Step 2: Now write define a variable using DIM as a Variant.
Sub VBA_ProjectPW2() Dim PW As Variant End Sub
Step 3: In the defined variable PW we will use the Input Box application and choose the title and message which we would like to see.
Sub VBA_ProjectPW2() Dim PW As Variant PW = Application.InputBox("Enter Password", "Password Protected") End Sub
Step 4: Open the Select Case-Else loop and there write the condition where if the entered password is “Pa$$w0rd123” then we would get the message as secured workbook in select range cell.
Sub VBA_ProjectPW2() Dim PW As Variant PW = Application.InputBox("Enter Password", "Password Protected") Select Case PW Case Is = False Case Is = "Pa$$w0rd123" Range("A1").Value = "Workbook is secured." Case Else End Sub
Step 5: And to see if the entered password is incorrect then we would get a message as Incorrect Password.
Sub VBA_ProjectPW2() Dim PW As Variant PW = Application.InputBox("Enter Password", "Password Protected") Select Case PW Case Is = False Case Is = "Pa$$w0rd123" Range("A1").Value = "Workbook is secured." Case Else MsgBox "Incorrect Password" End Select End Sub
Step 6: Now if we run the code by pressing F5 or the Play Button is mentioned below the menu bar, we would get the Password Protected window where we would enter the password. If the password entered is correct then this window will be closed.
Step 7: Once entered, click on Ok.
Step 8: We would see the code has run successfully. And the cell A1 would have the message which chose in VBA Code as “Workbook is secured.”
Pros of VBA Project Password
- This is helpful is securing the very confidential code which we probably do not want to show to anyone.
- VBA Project protected using the methods which we have seen in the above examples is best to secure the code.
Things to Remember
- Password entered using VBA Project Password can be cracked using the alternate add-ins available and can be downloaded from the internet or Microsoft website.
- Make sure the password entered is easy to enter, easy to remember. Or else keep the note so that it is not lost. If by any chance the entered password is incorrect then it is hard to recover it.
- If the excel is not saved in the format of Macro Enable format then the written code will be lost.
- VBA Project Password is different from the method of protecting the Excel file using Protect Sheet, Protect Workbook available in Review menu bar.
This is a guide to the VBA Project Password. Here we discuss how to protect the project code using the password in excel VBA along with practical examples and a downloadable excel template. You can also go through our other suggested articles –