Excel VBA-绕过我自己需要密码才能保存的代码,或者将密码从一个子系统传递到另一个子系统



我有一个Excel文件,里面有一些复杂的函数,所以我设置了以下代码,以防止我的一些同事在保存文件时做出意外的更改。直截了当,而且行之有效。不幸的是,我智胜了自己。

同一个文件偶尔需要半手动更新,从我们的网络驱动器中提取文件名信息。我写了一个宏来实现这一点。它在用户单击按钮时运行。[这是半手动的,因为我们在不同的大陆工作,虽然更新在法国需要90秒,但在美国需要30分钟。]这也有效。

我的问题是,我希望每个人都能运行宏更新,然后允许文件自动保存,但我不想把密码发给小组中的每个人。目前,更新宏在遇到"beforesave"代码时会暂停。有没有办法去

  1. 在从更新宏运行保存时绕过此密码请求,或者
  2. 将密码放入更新宏中,使"beforesave"代码能够识别并接受它

代码:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Password As String Dim EnteredPassword As String
Password = "NOTMYACTUALPASSWORD" 
EnteredPassword = InputBox("Enter password to save changes") 
If EnteredPassword <> Password Then 
Cancel = True 
MsgBox ("Password incorrect, file not saved") 
End If
End Sub

您不能告诉Excel跳过BeforeSave块,但您可以在代码停止保存之前退出代码。

' In a new module
Public Update_Active As Boolean ' defaults to False when not set
' At the end of your update macro before save is called
Update_Enable = True
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Update_Active Then
Update_Active = False
Exit Sub
End If
Dim Password As String Dim EnteredPassword As String
Password = "NOTMYACTUALPASSWORD" 
EnteredPassword = InputBox("Enter password to save changes") 
If EnteredPassword <> Password Then 
Cancel = True 
MsgBox ("Password incorrect, file not saved") 
End If
End Sub

最新更新