VBA Project Password

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.

Excel VBA Project Password 1

After entering the password, you will be able to view its properties and make changes to it. In this case, the password is “1234.”

Excel VBA Project Password 1-1

Until you give out the password, you cannot view the properties.

Excel VBA Project Password 1-2
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.

How to Password Protect the VBA Project 1

Select “Visual Basic” below the Developer section.

How to Password Protect the VBA Project 1-1
How to Password Protect the VBA Project 1-2

Select “Insert” then “Module” in the drop-down. Now, you can protect the module.

How to Password Protect the VBA Project 1-3

Step 2: Right-click on the module to be protected and select “VBAProject Properties.”

How to Password Protect the VBA Project 1-4

Step 3: Go to the “Protection” part in the properties.

How to Password Protect the VBA Project 1-5

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.

How to Password Protect the VBA Project 1-6

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.

How to Password Protect the VBA Project 1-7

It will open a dialog box to enter your password.

How to Password Protect the VBA Project 1-8

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 Password Protect the VBA Project 1-9

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.

How to Give Password Input Box to Run the Code 1

After you click the Developer icon, select “Visual Basic.” It opens the VBA Editor.

How to Give Password Input Box to Run the Code 1-1

But, if you open a new file, it will be empty. You’ll need to insert new modules.

How to Give Password Input Box to Run the Code 1-2

In the VBA toolbar, select “Insert,” then click “Module.”

How to Give Password Input Box to Run the Code 1-3

Step 2: Define a sub-procedure to run a VBA code if the right password is given.

How to Give Password Input Box to Run the Code 1-4

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.”

How to Give Password Input Box to Run the Code 1-5

Step 4: Define a Select Case statement to check if the given password matches or not.

How to Give Password Input Box to Run the Code 1-6

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.”

How to Give Password Input Box to Run the Code 1-7

Step 6: Declare an Else case in case the user has given out the wrong password.

How to Give Password Input Box to Run the Code 1-8

Then, end the Select case conditional statements.

How to Give Password Input Box to Run the Code 1-9

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.

How to Give Password Input Box to Run the Code 1-10

If it’s correct,

How to Give Password Input Box to Run the Code 1-11

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.

VBA Project Password Example 1

There are four sheets. But “Sheet2” is invisible. With the correct password, you can make it publicly visible.

VBA Project Password Example 1-1

Step 1: Define the subroutine to make an invisible sheet or a sheet with the property “xlSheetHidden” visible again.

VBA Project Password Example 1-2

Step 2: Store the password of the sheet in a string variable. Here, the password is “vba123”.

VBA Project Password Example 1-3

Step 3: Get the password input from the user using the InputBox function.

VBA Project Password Example 1-4

Step 4: Define a conditional statement to check if both the strings are equal or not.

VBA Project Password Example 1-5

Execute the If statement if it is true.

Step 5: Change the property of the sheet’s visibility to “xlSheetVisible”.

VBA Project Password Example 1-6

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.

VBA Project Password Example 1-7

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:

VBA Project Password Example 1-8
VBA Project Password Example 1-9
VBA Project Password Example 1-10

“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.

VBA Project Password Example 2

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.

VBA Project Password Example 2-1

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.”

VBA Project Password Example 2-2

Step 3: Define an If-Else conditional statement to check if the given user input matches the password to unprotect the sheet.

VBA Project Password Example 2-3

Step 4: If the passwords match, the Unprotect function is called with the password given to it.

VBA Project Password Example 2-4

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.

VBA Project Password Example 2-5

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.

VBA Project Password Example 2-6

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.

VBA Project Password Example 2-7

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.

VBA Project Password Example 3

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.

VBA Project Password Example 3-1

Step 2: Get the password from the user and store the input in a string variable.

VBA Project Password Example 3-2

Step 3: Define an If-Else statement in VBA to check if the passwords are similar or not.

VBA Project Password Example 3-3

Step 4: Define the If-condition by selecting the sheet where the expenses table is.

VBA Project Password Example 3-4

Step 5: Get the sum of the revenue using the VBA SUM function.

VBA Project Password Example 3-5

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.

VBA Project Password Example 3-6

Step 7: Get the value of the income gained by subtracting the total expenses from the total revenue.

VBA Project Password Example 3-7

Step 8: Print the total income, total expenses, and the total revenue gained using the MsgBox function.

VBA Project Password Example 3-8

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.

VBA Project Password Example 3-9

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.

VBA Project Password Example 3-10
VBA Project Password Example 3-11
VBA Project Password Example 3-12

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)

1) Can I remove or reset a forgotten VBA Project Password?

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. .

2) Are there alternatives to VBA Project Passwords for securing my code?

● 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.

3) Can I share a VBA-protected file without sharing the password?

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.

4) Are there any risks associated with using VBA Project Passwords?

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.

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 –

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *