如何运行只有一台计算机的macros

我有一个VBA表格。 我想知道是否有办法让它只在一台计算机上运行,​​这样如果有人想复制它,就不能在另一台计算机上运行。 我想也许我们可以使用计算机的mac地址作为安全模式。

任何人?


编辑:从miroxlav的答案实施的例子:

Const AllowedName As String = "CEB-L1-7440236" Sub UserForm_click() If Environ$("COMPUTERNAME") <> AllowedName Then MsgBox ("Not authorized.") Exit Sub End If If vbYes <> MsgBox("Launch the macro?", vbYesNo) Then Exit Sub '--start of code what macro actually does '.... '.... '.... '--end of code what macro actually does MsgBox ("Finsihed.") End Sub 

  1. 将计算机名称embedded到您的VBA代码中并为其添加testing。
  2. 密码保护代码模块,以便其他人不能看到/修改它。

也许你会想要lockingmacros,而不是用户名,而不是用电脑名(所以授权用户可以在任何一台电脑上使用它),但它在你身上。

 ' computer name ? Environ$("COMPUTERNAME") ' alternative: user name ? Application.UserName 

示例 – 在macros模块的顶部插入Const行,并在Ifmacros结束之后添加IfEndIf

 Const AllowedName As String = "abcd" Sub UserForm_click() If Environ$("COMPUTERNAME") <> AllowedName Then MsgBox ("Not authorized.") Exit Sub End If '--keep your original code here-- End Sub 

但是这个可以使用命令行相对容易地避开:

 SET COMPUTERNAME=abcd 

一旦有人知道abcd是运行macros所需的正确的计算机名称。 也许更喜欢用户名检查。 相关的代码行将更改为:

 If Application.UserName <> AllowedName Then 

当然,你也可以用VBA得到电脑的MAC地址,只要search“mac地址vba”,就有很多例子。