Locking VBA code in Excel prevents unauthorized users from viewing, modifying, or deleting your macros and modules. This is essential when distributing workbooks that rely on automated processes, such as data validation, report generation, or custom functions. Once locked, the code can only be accessed by entering the correct password. Before proceeding, ensure you have saved a backup of your workbook — forgetting the password means permanent loss of access to your code. You must also have administrative rights to modify VBA project properties.
What You'll Need
- An Excel file (.xlsx or .xlsm) containing VBA code
- Microsoft Excel 2010 or later (Windows or Mac)
- A strong, memorable password you are willing to remember
- Administrative access to the computer
Step-by-Step
- Open the Excel workbook containing the VBA code you want to lock.
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. This opens the project window on the left, listing all modules, forms, and class modules in your workbook.
- In the Project Explorer window, right-click on the name of your VBA project (usually named VBAProject (YourWorkbookName.xlsm)).
- Select Properties from the context menu. A Properties window will appear, typically docked on the lower left side of the VBA editor.
- In the Properties window, locate the field labeled Protection and click the checkbox next to Lock project for viewing.
- When prompted, enter a password in the Password field. Re-enter the same password in the Confirm password field to verify.
- Click OK. You will be asked to confirm the password again — type it once more and click OK.
- Close the VBA editor by clicking the X in the top-right corner. Return to Excel.
- Save the workbook as a macro-enabled file (.xlsm). If you saved it as .xlsx, the VBA code will be removed upon saving. Click File > Save As, then choose Excel Macro-Enabled Workbook (*.xlsm) from the file type dropdown.
- Close and reopen the workbook to test the lock. Press Alt + F11 again. You should now see a prompt asking for the password to view the project. Without the password, the code remains hidden and uneditable.
Common Mistakes
- Forgetting to save as .xlsm — this removes all VBA code and renders the lock useless.
- Using a weak or easily guessable password — this defeats the purpose of protection.
- Typing different passwords in the confirmation field — the system will reject the lock attempt.
- Locking the project but not protecting individual modules — while locking the project hides the code, unprotected modules can still be copied and pasted into other workbooks if the project is unlocked.
- Assuming the lock prevents macro execution — it only prevents viewing and editing. Macros still run normally when enabled.
Tips
- Store your password in a secure password manager — never write it on paper or send it via email.
- Use a password that includes uppercase, lowercase, numbers, and symbols (e.g., VbaLock2024!)
- Test the lock on a copy of your workbook before distributing the final version.
- Include a note in the workbook (e.g., on the first sheet) explaining that macros are required and providing instructions for enabling them if disabled.
- Consider using Excel’s built-in worksheet protection alongside VBA locking for layered security.
- On Mac, the menu path is identical but the interface may appear slightly different — the steps remain the same.
Troubleshooting
- If you can’t access the VBA editor after locking: Ensure macros are enabled in Excel. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and confirm Enable all macros (temporarily for testing) or Enable macros for this session.
- If the password prompt doesn’t appear: Double-check that you clicked Lock project for viewing and not just protected a module. The lock must be applied to the project itself, not individual components.
- If you forget the password: There is no official way to recover it. Microsoft does not provide password recovery for locked VBA projects. Your only option is to restore from a backup before locking or recreate the code from scratch.
- If the lock doesn’t persist after saving: You likely saved as .xlsx instead of .xlsm. Always verify the file extension before closing the workbook.
- If Excel crashes when opening the file: The workbook may be corrupted. Try opening it in Safe Mode by holding Ctrl while launching Excel. If successful, re-lock the code and save again.