Yes, you can protect VBA code in Excel by locking the VBA project with a password, which prevents others from viewing or editing the macros. However, this protection is not cryptographically strong—determined users can bypass it with specialized tools—so treat it as a deterrent, not absolute security.
Why You Might Want to Protect VBA Code
VBA (Visual Basic for Applications) code often contains business logic, custom functions, or automation that you don’t want competitors or casual users to copy. Protecting the VBA project is the built-in way to hide the code from prying eyes. It also prevents accidental or intentional changes that could break a workbook’s functionality. For example, a company might share a budgeting tool with employees but want to keep the underlying formulas and macros secret.
Note that protecting VBA code is separate from protecting the workbook structure or worksheet cells. You can lock a worksheet to prevent edits, but that doesn’t hide the VBA code. To hide the code itself, you must set a password on the VBA project.
How to Protect VBA Code (Step-by-Step)
Here’s how to set a password on the VBA project in Excel for Microsoft 365 on Windows:
- Open the workbook that contains the VBA code.
- Press Alt + F11 to open the Visual Basic for Applications editor. On a Mac, press Option + F11 or go to Tools > Macro > Visual Basic Editor.
- In the VBA editor, go to Tools > VBAProject Properties.
- Click the Protection tab.
- Check the box that says Lock project for viewing.
- Enter a password in the Password field and confirm it in the Confirm password field.
- Click OK.
- Save the workbook (Ctrl+S on Windows, Cmd+S on Mac). Close and reopen the workbook to ensure the protection takes effect.
After this, when someone tries to view the code, they’ll be prompted for the password. Without it, the code remains hidden and uneditable.
What Protection Actually Does (and Doesn’t Do)
Locking the VBA project encrypts the code with a password, but it’s a weak encryption compared to modern standards. The password is stored in the file and can be extracted with third-party password recovery tools or by hex-editing the file. In practice, this means the protection is effective against non-technical users, but a determined person with the right tools can remove the password in minutes.
It’s also possible to bypass the protection by editing the file’s binary structure, though this is illegal if you don’t own the code. For most business scenarios, the built-in protection is sufficient to keep honest people honest, but don’t rely on it for trade secrets.
If you need stronger protection, consider compiling the VBA code into an add-in (XLAM file) and distributing that, but even then, the code can be extracted with specialized software. There is no truly unhackable way to protect VBA code in Excel.
Common Problems and How to Avoid Them
Many users set a password and then forget it. There is no built-in “forgot password” recovery in Excel, so you could lose access to your own code. Always keep a backup copy of the workbook without the password, or store the password in a secure password manager.
Another issue is that the password only applies to the VBA project, not to the workbook’s worksheet protection. If you want to prevent users from changing formulas or data, you must also protect the worksheets via Review > Protect Sheet.
Also, note that the protection doesn’t hide the existence of macros. Users can still see the macro names via the Macros dialog (Alt+F8), but they can’t view the code.
Alternatives to Password-Protecting VBA
If your goal is to prevent users from seeing or modifying code, password protection is the standard method. But if you want to prevent users from running macros at all, you can digitally sign the code or set macro security settings. Here are some alternatives:
- Digitally sign the VBA project – This ensures the code hasn’t been tampered with, but doesn’t hide it.
- Disable macros on open – You can force users to enable macros manually, but that doesn’t protect the code.
- Use an add-in – Distribute the code as an add-in (XLAM) to make it less accessible, but it’s still extractable.
- Rewrite in another language – For critical logic, consider moving it to a server-side script or a compiled DLL, which is harder to reverse-engineer.
Comparing VBA Protection with Other Excel Security
Excel has multiple layers of protection, and they serve different purposes:
- Worksheet protection – Prevents users from editing cells, but doesn’t hide formulas or VBA.
- Workbook structure protection – Prevents users from adding, moving, or deleting sheets.
- File encryption – Requires a password to open the file at all, which is stronger than VBA project protection.
- VBA project protection – Hides the code from view, but is the weakest form of protection.
For maximum security, you can combine file encryption with VBA project protection. However, even file encryption can be cracked with brute-force tools, so it’s not absolute.
Frequently Asked Questions
Can I recover a forgotten VBA password? No, there is no official way to recover it. You can try third-party tools, but they may not work and could be illegal if you don’t own the code. Always keep a backup copy without the password.
Does protecting VBA code slow down my workbook? No, the protection only affects viewing/editing the code, not the execution speed. Macros run at the same speed whether or not the project is locked.
Can I protect VBA code in Excel for Mac? Yes, the steps are similar. In the VBA editor, go to Tools > VBAProject Properties, then the Protection tab. The interface is the same as on Windows.
Is it possible to hide VBA code without a password? No, the only built-in way is to lock the project for viewing, which requires a password. You can also hide the macro names by making them private, but the code is still viewable.
Will protecting VBA code prevent users from copying formulas in cells? No, that’s a separate issue. To hide formulas, you need to protect the worksheet and check the “Hidden” option in the Format Cells dialog.
Final Thoughts on VBA Protection
Protecting VBA code is a simple process that adds a layer of confidentiality to your macros. It’s perfect for keeping casual users from viewing or altering your code. However, be aware of its limitations: it’s not strong encryption, and a skilled person can bypass it. Always keep a backup of your code, and use strong passwords that you can remember or store securely.
Related
- Excel How to Ungroup Cells: Rows, Columns & Sheets
- How to Unmerge Cells in Excel (Group of Cells)
- Excel HR Templates for Attendance: A Practical Guide
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- MS Access VBA: Open Excel Files and Read Their DataYes. In Microsoft Access VBA on Windows, you can open an Excel workbook by creating an Excel application…
- Excel VBA Set Cell Color by Hex CodeTo set an Excel cell’s fill color from a hex value such as #1E90FF, convert its red, green,…
- VBA Assign String to Variable: Syntax and ExamplesTo assign text to a VBA variable, declare it as a string with Dim and use the equals…
- VBA: Check Whether a String Contains TextYes. In VBA, use the InStr function to check whether one string contains another: it returns the position…
- VBA Loop Through a PivotTable: Examples and TipsTo loop through a PivotTable in Excel VBA, assign it to a PivotTable variable and use a For…
- VBA Import CSV in Excel: Methods, Code, and FixesTo import a CSV file with VBA in Excel for Microsoft 365 on Windows, use Workbooks.OpenText to parse…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.