What is Excel VBA Project Password?
Excel VBA Project Password is used to set a password to protect the VBA code within a workbook. When you set a password for a VBA project, it prevents unauthorized access to and modification of the underlying VBA code. It can be helpful in scenarios where you want to protect the intellectual property of your macros or prevent others from viewing or modifying the code.
It is also essential to keep a record of these passwords if you have the VBA Project Password forgot password problem. Consider the following example. Once you open the VBA Module, to view the code, you can type the password.
After entering the password, you will be able to view its properties and make changes to it. In this case, the password is “1234.”
Until you give out the password, you cannot view the properties.
Table of Contents
Key Takeaways
- VBA Project passwords provide a level of protection against unauthorized access to the Visual Basic for Applications (VBA) code in an Excel workbook.
- You can set a password for the VBA Project by accessing the VBA editor, right-clicking on the project, selecting “VBAProject Properties,” and going to the “Protection” tab.
- VBA Project passwords are often used to protect the intellectual property of macros and custom functions developed in VBA.
- Forgetting the VBA Project password can result in the loss of access to the VBA code.
- Input Boxes in VBA allow users to enter passwords interactively, enabling a dynamic and user-friendly way to handle password input.
How to Password Protect the VBA Project?
To see how to Protect your VBA module with Password protection, follow the steps below.
Step 1: Open the Developer tab in the VBA module. It will be available in the Excel toolbar.
Select “Visual Basic” below the Developer section.
Select “Insert” then “Module” in the drop-down. Now, you can protect the module.
Step 2: Right-click on the module to be protected and select “VBAProject Properties.”
Step 3: Go to the “Protection” part in the properties.
You should give it a password of your choice. To prevent the case of the VBA Project Password forgot problem, make a password easy and straightforward to remember. Or else keep records of it.
Step 4: Mark the “Lock project for viewing option.” Now, close and reopen the Excel file. Then, go to the VBA Editor.
As you can see, none of the VBA codes are seen. Only if you enter the password correctly will you be able to view the contents in Excel VBA.
Step 6: Click on the “+” sign in the VBA File.
It will open a dialog box to enter your password.
Step 7: Type in the password to perform the VBA Project password crack. Now you’re in! You can successfully view the modules and objects of your choice.
How to Give Password Input Box to Run the Code?
To run a VBA code with password protection given to it, follow the steps given below.
Step 1: Once you open the Excel Workbook, in the title bar, choose the “Developer” icon and click on it.
After you click the Developer icon, select “Visual Basic.” It opens the VBA Editor.
But, if you open a new file, it will be empty. You’ll need to insert new modules.
In the VBA toolbar, select “Insert,” then click “Module.”
Step 2: Define a sub-procedure to run a VBA code if the right password is given.
Step 3: Define a Variant variable for the password. Then, get the password from the user with the Application.InputBox function. The title of the Input box is “Password Protected.”
Step 4: Define a Select Case statement to check if the given password matches or not.
Step 5: Check if the string matches the password for the VBA code to run. If this is true, it will print the above string in cell “A1.”
Step 6: Declare an Else case in case the user has given out the wrong password.
Then, end the Select case conditional statements.
Code:
Sub VBA_ProjectProtect()
Dim PW As Variant
PW = Application.InputBox(“Enter Password”, “Password Protected”)
Select Case PW
Case Is = “password”
Range(“A1”).Value = “Workbook is secured.”
Case Else
MsgBox “Incorrect Password”
End Select
End Sub
Step 7: Click “F5” or the “Run” icon on the activity bar in the Excel VBA Module to run the subroutine. The results are seen below.
If it’s correct,
Examples
See some interesting examples of how to use the VBA Project password to view and edit VBA and Excel projects.
Example #1
Suppose a sheet in your Excel file is invisible to view. You can view it by creating a VBA subroutine to make it visible publicly. See how to do that by following the steps below.
There are four sheets. But “Sheet2” is invisible. With the correct password, you can make it publicly visible.
Step 1: Define the subroutine to make an invisible sheet or a sheet with the property “xlSheetHidden” visible again.
Step 2: Store the password of the sheet in a string variable. Here, the password is “vba123”.
Step 3: Get the password input from the user using the InputBox function.
Step 4: Define a conditional statement to check if both the strings are equal or not.
Execute the If statement if it is true.
Step 5: Change the property of the sheet’s visibility to “xlSheetVisible”.
Define a message box to confirm the process is happening successfully.
Step 6: If the password is wrong, it will execute the Else part of the conditional statement.
Code:
Sub UnlockSheetWithPassword()
Dim correctPassword As String
correctPassword = “vba123”
Dim enteredPassword As String
enteredPassword = InputBox(“Enter the password to unlock the sheet:”, “Password Required”)
If enteredPassword = correctPassword Then
Worksheets(“Sheet2”).Visible = xlSheetVisible
MsgBox “Sheet unlocked. You can now view the sensitive information.”
Else
MsgBox “Incorrect password. Sheet will not be unlocked.”
End If
End Sub
Step 10: Press “F5” to run the code. The output is displayed as shown below:
“Sheet2” is currently visible.
Example #2
Consider a protected sheet. A protected sheet is when you cannot edit the cells in a worksheet unless you know the password to un-protect it.
With the VBA Project password, you can create a subroutine to unprotect your worksheet, provided the correct password is given.
You can protect your worksheet by going to the “Review” part in the Excel Toolbar and selecting “Protect Worksheet.”
Step 1: Start with naming a subroutine to unprotect a given sheet by giving its password.
Step 2: Get a password input from the user and store it in a string variable. The title of this input box is “Password Required.”
Step 3: Define an If-Else conditional statement to check if the given user input matches the password to unprotect the sheet.
Step 4: If the passwords match, the Unprotect function is called with the password given to it.
Step 5: If the passwords don’t match, define an Else statement to show what to do otherwise. To signify that the process has been done, you can confirm it by printing it in a message box.
Code:
Sub UnprotectSheetWithPassword()
Dim enteredPassword As String
enteredPassword = InputBox(“Enter the sheet password:”, “Password Required”)
If enteredPassword = “tutorials123” Then
Sheets(“Sheet4″).Unprotect Password:=”tutorials123”
MsgBox “Sheet unprotected. You can now view the sensitive information.”
Else
MsgBox “Incorrect sheet password. The sheet will not be unprotected.”
End If
End Sub
Step 9: Run the code, and it will ask for the password to unprotect the sheet.
The sheet is now unprotected if you go through the Review part in the Excel Toolbar. It will show “Protect Worksheet”. It means that the current worksheet is not protected.
Example #3
Suppose you want to find the total income generated, but it is a secret; you can make the financial summary statements private information using VBA Project passwords.
Consider a table with columns for Revenue and expenditure.
You can get the net income by subtracting the total revenue from the total expenses incurred.
Step 1: Define a subroutine to get the financial summary of the given table with a password check.
Step 2: Get the password from the user and store the input in a string variable.
Step 3: Define an If-Else statement in VBA to check if the passwords are similar or not.
Step 4: Define the If-condition by selecting the sheet where the expenses table is.
Step 5: Get the sum of the revenue using the VBA SUM function.
It adds all the values in the Revenue column till the last non-empty cell. It is done using the VBA xlUp function, which simulates the “Up” arrow key.
Step 6: Get all the expenses incurred by adding all values in the Expenses table similarly.
Step 7: Get the value of the income gained by subtracting the total expenses from the total revenue.
Step 8: Print the total income, total expenses, and the total revenue gained using the MsgBox function.
To represent a newline character sequence in VBA, you utilize the vbCrLf constant.
When generating multiline text or formatting the output with line breaks in excel, it’s used to insert a line break or carriage return in strings.
The control characters “CrLf” stand for Carriage Return and Line Feed, used to mark the end of a line and advance the cursor to the start of the following line.
Step 9: Define an Else condition to be executed if the passwords are incorrect.
Code:
Sub DisplayFinancialSummary()
Dim enteredPassword As String
enteredPassword = InputBox(“Enter the VBA project password:”, “Password Required”)
If enteredPassword = “finance123” Then
MsgBox “Financial summary calculation in progress…”
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(“Sheet3”)
Dim totalRevenue As Double
totalRevenue = Application.WorksheetFunction.Sum(ws.Range(“B2:B” & ws.Cells(ws.Rows.Count, “B”).End(xlUp).Row))
Dim totalExpenses As Double
totalExpenses = Application.WorksheetFunction.Sum(ws.Range(“C2:C” & ws.Cells(ws.Rows.Count, “C”).End(xlUp).Row))
Dim netIncome As Double
netIncome = totalRevenue – totalExpenses
MsgBox “Total Revenue: $” & totalRevenue & vbCrLf & _
“Total Expenses: $” & totalExpenses & vbCrLf & _
“Net Income: $” & netIncome
Else
MsgBox “Incorrect VBA project password. Access denied.”
End If
End Sub
Step 10: Run the above VBA subroutine to get the financial statement of the given sales data.
Thus, you’ve successfully printed the financial statement of the given data.
Important Things To Note
- Choose a strong and secure password for your VBA Project. Keep a secure record of passwords in case of forgetfulness.
- Avoid hardcoding sensitive information directly into your VBA code.
- Refrain from using easily guessable or common passwords.
Frequently Asked Questions (FAQs)
Yes, you can remove or reset the password if you have forgotten your VBA Project Password. Here are the steps on how to do that by changing the file extension to crack the password.
Step 1: Convert the ‘.xlsm’ file to ‘.zip’ file.
Step 2: With an Archiver program (WinRAR), view the files in the zip file.
Step 3: In the XL directory, select “vbaProject.bin.”
Step 4: With any hex editor you have (or install it), query “DPB=” in the file opened in the hex editor.
Step 5: Once located, delete “DPB=” and then replace it with “DPX=”. Save and close your hex editor.
Step 6: Overwrite the current “vbaProject.bin” with the new edited file with the same name.
Step 7: Change the zip file back to the XLSM file. Select “Yes” when a warning window pops up.
Step 8: Open the VBA editor and click “OK” if any dialog boxes come up.
Step 9: Right-click on the project and click on “VBProperties”. Go to the protection tab, disable the “Lock Project for Viewing” option, and save the changes. .
● Use Excel’s built-in workbook protection features to limit access and editing.
● Digitally sign your VBA code to verify its authenticity.
● Convert VBA code into compiled add-ins for added security.
● Implement sensitive functionality in external dynamic-link libraries (DLLs).
● Encrypt sensitive data within your code.
You cannot share a VBA-protected file without sharing the password directly. However, there are some roundabout ways to do it,
● Compile to Add-In: Compile your VBA code into an add-in to share functionality without exposing the original code or password.
● Use External Database: Store sensitive information externally and connect your VBA code to that database.
● Deploy Executable: Develop a standalone executable that interacts with your VBA code but doesn’t require the password for end-users.
● Encrypted Communication: Establish secure communication methods for accessing the VBA functionality remotely.
There are many risks concerning VBA Project Passwords. Some of them are.
● They provide only limited security for your VBA Project.
● Passwords do not encrypt the code; they only prevent direct access to it.
● Forgetting the password may result in loss of access to the VBA code resulting in you needing to perform VBA Project Password recovery.
● Passwords provide all-or-nothing access, lacking granular control.
● Using third-party tools to remove passwords may pose security risks.
Recommended Articles
This has been a guide to VBA Project Password. Here we learn How to Password Protect the VBA Project along with examples. You can learn more from the following articles –
Leave a Reply