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"
- Now, under the Tools tab, click VBAProject Properties.
- On the Protection tab, check the “Lock project for viewing” option and enter a password of your choice, twice.
- 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.
- 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
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