你如何密码保护Excel VBAmacros运行

我刚刚写完我的代码,并在表格中添加了一个button。 我希望有一种方法可以保护button免受“错过使用”,因为它只适用于一个人。

你可以使用环境variables检查你的特殊用户: Environ$("computername")如果你想通过计算机检查或Environ$("username")如果你对login用户名感兴趣。 (或者同时在一台计算机上同时使用一个用户)。

然后为了额外的安全密码保护你的VBA代码。

编辑:

我刚刚意识到我没有回答这个问题。 (或者我回答了我想要的)。 如果你想通过密码来保护你的macros,一个解决scheme就是让你的button调用一个带有TextBox1CommandButton1窗体。

然后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 

希望一个解决scheme或另一个是有帮助的。