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

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you’ll learn how to Find Blank cells in excel so that you can remove or delete the blank cells form...
In this article, you’ll learn what is a Gauge Chart in Microsoft Excel. Also, you will learn how to add...
Microsoft Excel provides a shortcut for the users to move columns in excel using two different ways – using Shift...