How to Password Protect an Excel Macro?

In this post, you’ll learn about password protecting an Excel Macro using VBA so that you can secure your Excel Macro workbook.

How to Password Protect an Excel Macro?

You can protect your macros with the help of a password, just like protecting files and sheets. To start with place a command button on your worksheet and add the following code lines:

  • Create a simple macro in the file.
Range("A1").Value = "Password Protect Macro"
How to Password Protect an Excel Macro?
  • Now, under the Tools tab, click VBAProject Properties.
How to Password Protect an Excel Macro?
  • On the Protection tab, check the “Lock project for viewing” option and enter a password of your choice, twice.
How to Password Protect an Excel Macro?
  • Click OK.
  • Save and close the file.
  • Now, reopen the Excel file.
  • Try to view the code.
  • A Dialog box will appear which requires a password.
How to Password Protect an Excel Macro?
  • But you can still execute the code by clicking on the command button.
  • But you cannot view or edit the code.

You can do the same by entering the code below in the VBA

Code:

Dim password As Variant
password = Application.InputBox("Enter Password", "Password Protected")

Select Case password
    Case Is = False
        'do nothing
    Case Is = "password"
        Range("A1").Value = "This is secret code"
    Case Else
        MsgBox "Incorrect Password"
End Select
Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Hey, wait!Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.