I confess that today’s tip was motivated by a student asking a question in a recent class. During a discussion about all of the different ways you can password protect a document, the question was asked about how to password protect just a macro.
This technique will allow you to protect your macro code from snoops and ne’er-do-wells, but still allow the document to be opened without a password AND the macro will run without a password. In order to perform these techniques, you’ll need to know how to Turn on the Developer Tab and Record a Simple Macro. By necessity we’ll be dipping into the Visual Basic Editor, but only for a moment.
Using VBE to Password Protect your Macro
- Create your macro. For this example I’m going to record a simple macro that enters my first name. The steps to do that are as follows:
- Click Record Macro in the Code group on the Developer tab.
- Enter the Name example (no spaces allowed).
- Enter the Description “This is an example.”
- Click OK.
- Enter your first name in cell A1.
- Click Stop Recording in the Code group on the Developer tab.
- Save the document in a macro-enabled format.
- Click the Visual Basic button in the Code group on the Developer tab to open the Visual Basic Editor (VBE).
- Click the Tools menu.
- Choose VBA Project Properties from the dropdown.
- Click the Protection tab in the VBA Project Properties window.
- Place a checkmark beside Lock Project for Viewing.
- Enter the Password twice in the fields.
- Click OK.
- Close the VBE window or press Alt + Q.
- Save your Excel document.
- Close and then re-Open the document.
- Click your mouse in cell A5.
- Click the Macros button in the Code group on the Developer tab.
- Click the name of the Macro you just created and then click Run.
- Observe that the Macro ran correctly.
- Click the Visual Basic button in the Code group on the Developer tab to open the Visual Basic Editor (VBE).
- Now you won’t be able to see your code unless you enter the password.