3 Ways to Protect VBA Code

Table of contents:

3 Ways to Protect VBA Code
3 Ways to Protect VBA Code
Anonim

Microsoft Visual Basic for Applications (VBA) is a high-level programming language that allows you to write programs to automate functions and tasks within Microsoft Office. This article shows you how to secure your VBA code so that other users cannot modify or copy it.

Steps

Method 1 of 3: Password Protect the VBA Code

Protect VBA Code Step 1
Protect VBA Code Step 1

Step 1. Open the Visual Basic Editor

Normally you can do this by accessing the "Tools" menu and choosing the "Macro" option (in Access, depending on your computer configuration, you may need to be inside the database window to access the editor).

  • Go to the "Tools" menu of the Visual Basic Editor and choose the "Properties" option.

    Protect VBA Code Step 1Bullet1
    Protect VBA Code Step 1Bullet1
Protect VBA Code Step 2
Protect VBA Code Step 2

Step 2. Go to the "Security" tab

Protect VBA Code Step 3
Protect VBA Code Step 3

Step 3. Select the "Lock project for viewing" check button

If you do not tick this checkbox, your code will not be hidden and protected from prying eyes.

Protect VBA Code Step 4
Protect VBA Code Step 4

Step 4. Create a login password using the appropriate fields, then enter it again for confirmation

Protect VBA Code Step 5
Protect VBA Code Step 5

Step 5. Save your changes and restart your program for the new settings to take effect (in Microsoft Excel 2007 and later versions, you may need to save your work in "XLSM" format for your code to work correctly)

Method 2 of 3: Hide VBA Code in Read Only Files Using Access 2007

Protect VBA Code Step 6
Protect VBA Code Step 6

Step 1. Go to the "Database Tools" tab

Protect VBA Code Step 7
Protect VBA Code Step 7

Step 2. Locate the "Database Tools" group

Protect VBA Code Step 8
Protect VBA Code Step 8

Step 3. Choose the "Create ACCDE" option

Protect VBA Code Step 9
Protect VBA Code Step 9

Step 4. Save the file in "ACCDE" format using a different name from the original

The new "ACCDE" file will be created as read-only, in order to make the necessary changes to your work you will therefore need to have the original file as well.

Method 3 of 3: Protect the VBA Code By Creating an Add-on

Protect VBA Code Step 10
Protect VBA Code Step 10

Step 1. Create an empty Office file, of the same type as the one that the VBA code will use (for example if your code works on an Excel sheet, create an empty Excel file)

Protect VBA Code Step 11
Protect VBA Code Step 11

Step 2. Copy the VBA code into the Visual Basic Editor of the new file

Protect VBA Code Step 12
Protect VBA Code Step 12

Step 3. Open the "Macro" window, normally available in the "Tools" menu

Protect VBA Code Step 13
Protect VBA Code Step 13

Step 4. Test your code again using debug, making sure everything is working correctly

Protect VBA Code Step 14
Protect VBA Code Step 14

Step 5. Delete any data entered in the new file to be able to test the VBA code

Protect VBA Code Step 15
Protect VBA Code Step 15

Step 6. Add a description to the macro that your add-on will run (you may need to select the "Options" item for your macro to be able to enter a description)

Protect VBA Code Step 16
Protect VBA Code Step 16

Step 7. Compile the VBA code (from the Visual Basic Editor window, access the "Debug" menu and choose the "Compile VBA project" option)

Protect VBA Code Step 17
Protect VBA Code Step 17

Step 8. Save a copy of the file in its standard format

Protect VBA Code Step 18
Protect VBA Code Step 18

Step 9. Access the "Tools" menu of the Visual Basic Editor window and choose the "Properties" option

Protect VBA Code Step 19
Protect VBA Code Step 19

Step 10. Select the "Security" tab

Protect VBA Code Step 20
Protect VBA Code Step 20

Step 11. Check the "Lock project for viewing" checkbox (depending on the file format you are working on and the settings of Microsoft Office and your computer, you may need to create a login password)

Protect VBA Code Step 21
Protect VBA Code Step 21

Step 12. Open the "Save As" or "Save a Copy" dialog box

Protect VBA Code Step 22
Protect VBA Code Step 22

Step 13. Access the file format drop-down menu and choose the appropriate one for the type of add-on you created

  • If you have created an add-in for Microsoft Word, use the "DOT" file format (if you want the add-in to run when Word starts, save it in your Office Startup folder).
  • If you have created an add-in for Microsoft Excel, use the "XLA" file format.
  • If you have created a Microsoft Access add-in, use the "MDE" file format to protect your VBA code (Microsoft Access add-ins can also be saved in "MDA" format, but in that case the VBA code will not be hidden).
  • If you have created a Microsoft PowerPoint add-in, use the "PPA" file format. In this case, you will be the only user who can view and edit the VBA code.
Protect VBA Code Step 23
Protect VBA Code Step 23

Step 14. Close and reopen Microsoft Office

You should now be able to use the add-on you created.

Advice

  • If you can't locate the VBA Editor or Add-ons Manager, make sure it is installed on your system. Otherwise, most likely, you will need to use the Microsoft Office installation disc to proceed with adding the programs in question.
  • Your Microsoft Office configuration and related settings can change where components and functions are located within each program. If you can't find a specific function, try a quick search in the "Help" using the name of the function in question.

Recommended: