简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM