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

  1. Open the Excel workbook containing the VBA code you want to lock.
  2. 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.
  3. In the Project Explorer window, right-click on the name of your VBA project (usually named VBAProject (YourWorkbookName.xlsm)).
  4. Select Properties from the context menu. A Properties window will appear, typically docked on the lower left side of the VBA editor.
  5. In the Properties window, locate the field labeled Protection and click the checkbox next to Lock project for viewing.
  6. When prompted, enter a password in the Password field. Re-enter the same password in the Confirm password field to verify.
  7. Click OK. You will be asked to confirm the password again — type it once more and click OK.
  8. Close the VBA editor by clicking the X in the top-right corner. Return to Excel.
  9. 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.
  10. 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.

Frequently Asked Questions

Can I unlock VBA code if I forget the password?

No. Excel does not provide a password recovery mechanism for locked VBA projects. Once forgotten, the code is permanently inaccessible unless you have a backup.

Does locking VBA code stop macros from running?

No. Locking only prevents users from viewing or editing the code. Macros will still execute normally if the user enables macros when opening the file.

Can I lock VBA code on a Mac?

Yes. The process is identical on macOS Excel. Use Alt + F11 to open the VBA editor, right-click the project, select Properties, and check Lock project for viewing.

Is VBA code locking secure against determined hackers?

It provides basic protection against casual users but is not unbreakable. Advanced users can use third-party tools or hex editors to remove the lock. For high-security environments, consider alternative automation methods or server-based solutions.

Can I lock multiple workbooks with the same password?

Yes. You can apply the same password to multiple workbooks, but each project must be locked individually. There is no global setting to apply one password across files.

Will locking the project affect performance or file size?

No. Locking adds no measurable overhead to file size or execution speed. It is a metadata flag that triggers the password prompt — not an encryption process.