Securing Your VBA Code

Don’t get excited, you can’t. There are no passwords in Excel that are truly secure, so if you need rock-solid security, Excel probably isn’t the tool for you. However, you can keep your code from casual prying eyes. In the Visual Basic Editor (VBE), right click your project and choose VBA Project Properties.

ProjProp

On the Protection tab, check the “Lock project for viewing” check box, type a password (twice) and you’re all set.

ProjProp2

For the love of Pete, don’t forget your password. There are commercial password crackers available, but it’s a lot easier to remember it.

I lock all my projects for viewing if they will always be open (like Personal.xls and Add-ins) because if I don’t, all their code windows keep opening up when I start the VBE. I hate that, and protecting the code prevents it. I use the same password for all of them because I’m not really trying to protect them, just keep them out of the way.

17 thoughts on “Securing Your VBA Code

  1. I’ve protected an access dbase and lost the password.
    What commercial crack can I use?

  2. after locking the project.It is allowing to save me the excel.
    Is there any way to hide the code allow the users to save the data in the excel.

  3. Tricks for remembering passwords (when they are less for absolute security, and more for preventing inadvertant changes).

    Worksheets: I like to apply protection with no password. Stops unintended changes, but doesn’t prevent the savvy user from working with the sheet.

    XLS File: Name the file with something like “Project XACV Labour Forecast”. When next opening the file, the password dialog comes up as “Project XACV Labour…” is protected” with a password entry textbox. The password is right there.

    VBE: Using [Project Name:] define an application name which is somehow symbolic of the proect’s code, let’s say gCCxA01. Then make the VBE password the same. When next opening the code in VBE the dialog says “gCCxA01 Password” with a password entry textbox. Just enter “gCCxA01?

    This approach obviously won’t keep out the safecrackers – but that is not the point in this case.

  4. Dear.
    I made an excel file and used VBA to make alot of functions with. I also protected the code in VBE (visual basic editor) from accessing and viewing, by using “Protect properties” tools. Unfortunatly, I lost my note and foget my password to show VBE again, I try to search alot on Net, but there is not any solutions. So its my trouble.
    How can I do to review my code.
    Thanks alot.
    Regards.
    John.

  5. Hi,
    I am trying to prevent users from viewing the macros in my Workbook. I have locked the Project via VBE & hidden the VB toolbar on workbook open, but the user can still see the macros by selecting view VB toolbar & run macro options. How do i lock this down so the macros can’t be run or only run via a password. Is there another way to protect my work.
    Thanks alot
    Cheers
    Tammy

  6. Tammy,

    I suppose the obvious answer is if the users don’t need to run the macros then don’t store them in that file. If you move them into an add-in the users won’t be able to see or run them unless then open the xla file.

    Failing that, you could add an inputbox that requires the user to enter a password before the code runs.

    Something like this:

    Top:
        Pass = InputBox(“Enter Password”)
        If Pass = “MyPassword” Then
            ‘insert code here…
       Else
            Msg = “Incorrect Password.  Try Again?”
            Style = vbYesNo + vbCritical + vbDefaultButton2
            Title = “Password Fail”
            Response = MsgBox(Msg, Style, Title)
            If Response = vbYes Then
                GoTo Top:
            Else
            End If
        End If
  7. Thanks Simon. I had a quick read about add-ins, i don’t think the end users can reference .xla’s (system securities) but i’ll check into it further. The user does need to run the macros with user input required & the sheets have alot of formulas, so i have protected the sheets and part of my vb project is the Protect & Unprotect vb code. I’ll continue reading more about add-ins.
    Thanks again for your quick response.
    Tammy

  8. Use an obfuscator. People will always be able to read the code, regardless of passwords.
    Also try the shared addin password for a vba addin if you want to protect your ip.

  9. Hi,
    I am trying to prevent users from viewing the macros in my Workbook. I have locked the Project via VBE & hidden the VB toolbar on workbook open, but the user can still see the macros by selecting view VB toolbar & run macro options. How do i lock this down so the macros can’t be run or only run via a password. Is there another way to protect my work.
    Thanks alot
    Cheers

  10. Hi, I have taken over a project for a coworker that is no longer employed with my company, and I have several VBE codes to work on in Excel, however, VBE in each workbook is locked with a password, which I am unfortunately without. Can you give me some advice for accessing VBE?
    many thanks!


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.