如何密码保护excel VBA宏运行 [英] How do you password protect excel VBA macro from running

查看:191
本文介绍了如何密码保护excel VBA宏运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚写完我的代码,并在表单中添加了一个按钮。我希望有一种方法来保护按钮免受小心使用的影响,因为它只适用于一个人。

解决方案

您可以使用环境变量来检查您的特殊用户: Environ $(computername)如果要通过计算机或 Environ $( username)如果您对登录用户名感兴趣(或者在一台电脑中使用两个用户)。



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



编辑:



我刚刚意识到我没有回答这个问题。 (或者我回答了我想要的)。
如果要通过密码保护您的宏,一个解决方案是让您的按钮调用一个用户窗体,一个 TextBox1 和一个 CommandButton1



然后UserForm中的代码应该是:

  Private Sub CommandButton1_Click()
如果TextBox1.Value =密码,然后用您的自定义密码替换密码
LaunchYourMacro这是您的按钮被调用的子。
Else
MsgBox你不能启动宏
退出Sub
End Sub

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


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.

这篇关于如何密码保护excel VBA宏运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆