Chapter 1.9 - Protect Macro
Chapter 1.9 - Protect Macro
#1 Excel tutorial on the net Excel Introduction Basics Functions Data Analysis VBA 300 Examples Ask us
Just like you can password protect workbooks and worksheets, you can password protect a macro in Excel from
being viewed (and executed). Chapter < >
Place a command button on your worksheet and add the following code lines: Create a Macro
Macro Recorder
FormulaR1C1
Macro Security
Protect Macro
3. On the Protection tab, check "Lock project for viewing" and enter a password twice.
4. Click OK.
5. Save, close and reopen the Excel file. Try to view the code.
You can still execute the code by clicking on the command button but you cannot view or edit the code anymore
(unless you know the password). The password for the downloadable Excel file is "easy".
6. If you want to password protect the macro from being executed, add the following code lines:
Explanation: The macro uses the InputBox method of the Application object. If the users clicks Cancel, this
method returns False and nothing happens (InputBox disappears). Only when the user knows the password
("easy" again), the secret code will be executed. If the entered password is incorrect, a MsgBox is displayed.
Note that the user cannot take a look at the password in the Visual Basic Editor because the project is protected
from being viewed.