Tito Paterson
Tito Paterson

Reputation: 307

How do you password protect excel VBA macro from running

I have just finished writing my code and have added a button to the sheet. I am hoping there is a way to protect the button from "miss use" as it is only meant for one person only.

Upvotes: 1

Views: 5595

Answers (1)

hstay
hstay

Reputation: 1439

You can use environment variables to check for your special user: Environ$("computername") if you want to check by computer or Environ$("username") if you are rather interested in Login username. (Or use both for one user in one computer).

Then for extra security password protect your VBA code.

EDIT:

I have just realised that I didn't answer the question. (Or I answered what I wanted). If you want to protect by password your macro, one solution is making your button call a userform with a TextBox1 and a CommandButton1.

Then the code in the UserForm should look:

Private Sub CommandButton1_Click()
    If TextBox1.Value = "Password" Then 'Replace Password by your custom password
        LaunchYourMacro 'This is the sub that was being called by your button.
    Else
        MsgBox "You are not allowed to launch the macro"
    Exit Sub
End Sub

Hope one solution or the other is helpful.

Upvotes: 2

Related Questions