简体   繁体   English

如何使用密码保护Excel VBA宏不运行

[英]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. 您可以使用环境变量来检查您的特殊用户:如果要通过计算机检查,请使用Environ$("computername") Environ$("username")如果您对登录用户名很感兴趣,则可以使用Environ$("username") (Or use both for one user in one computer). (或在一台计算机上同时为一个用户使用两个)。

Then for extra security password protect your VBA code. 然后,为了获得额外的安全密码,请保护您的VBA代码。

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 . 如果要通过密码保护宏,一种解决方案是使按钮使用TextBox1CommandButton1调用用户窗体。

Then the code in the UserForm should look: 然后,UserForm中的代码应如下所示:

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. 希望其中一个解决方案对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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