[Excel VBA] How to Protect Your Source Code (Setting a Project Password)

When sharing Excel files with macros, you may want to prevent others from viewing or accidentally editing your source code. To achieve this, you can lock the VBA project with a password.

By setting this protection, the code becomes inaccessible within the Visual Basic Editor (VBE) unless the correct password is provided.

目次

Steps to Set a VBA Project Password

1. Open the VBA Editor

Open your Excel file and press Alt + F11 to display the VBA Editor (VBE).

2. Open Project Properties

In the menu bar, click Tools > VBAProject Properties. (Note: “VBAProject” is the default name; it may differ if you have renamed your project.)

3. Navigate to the Protection Tab

In the dialog box that appears, select the Protection tab.

4. Configure the Lock and Password

  1. Check the box “Lock project for viewing”.
  2. Enter your desired password in the Password field.
  3. Re-enter the same password in the Confirm password field.

5. Save and Restart

Click OK to close the dialog. You must save the Excel file and close it for the protection to take effect. The lock becomes active the next time the file is opened.

Behavior After Locking

When someone tries to expand the VBA project folders in the VBE after the lock is active, a dialog box will appear stating: “Project is locked. Please enter password.” Without the password, the modules and code remain hidden.

Important Notes

  • Do not forget your password: If you lose it, you will be unable to view or edit your own code. There is no official way to recover it.
  • Security Level: This is primarily for “viewing prevention.” It is not a military-grade security measure, but it is effective for general business use.
  • Scope: Unlike worksheet protection, this setting locks all modules, forms, and class modules within the entire VBA project.

Summary

  • To prevent others from viewing VBA code, set a project password.
  • Access the setting via Tools > Properties > Protection tab.
  • The protection is finalized only after saving and restarting the file.

This is a highly effective method when sharing macro-enabled files while keeping your logic and source code private.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次