我在调用该事件的Workbook_BeforeSave时遇到问题,但是大多数代码没有任何作用。
我想做的是用密码保护某些工作表,隐藏另一工作表并激活第三个工作表。奇怪的是,子项中的某些项目(例如debug.print和MsgBox)。我还从Workbook_Open中激活了工作表,但是我尝试在Workbook_BeforeSave子代码中执行的大多数代码都不会触发。这是我要运行的代码:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.EnableEvents = False
Application.ScreenUpdating = False
If AdministratorAccess Then ' A boolean that is marked true when admin logs in
If MsgBox("For security, sheets must be locked and Admin logged out before saving. " & _
"Do you want to Save the workbook?", _
vbYesNo, "Changes not saved") = 6 Then
Call protectReadiness ' A sub that protects the 'Project Readiness' sheet
Call protectViewer ' A sub that protects the 'Project viewer' sheet
Call veryHideUsers ' A sub that xlVeryHides the 'Users' sheet
Sheets("Project viewer").Activate
MsgBox ("To continue editing as Admin, simply log back in.")
Else
Cancel = True
End If
Else
Call protectReadiness ' A sub that protects the 'Project Readiness' sheet
Call protectViewer ' A sub that protects the 'Project viewer' sheet
Call veryHideUsers ' A sub that xlVeryHides the 'Users' sheet
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
在上面的代码片段中,对“ protectReadiness”和Sheets(“ Project viewer”)。activate的调用根本没有运行。当我将它们添加到立即窗口时,它们可以正常工作。 MsgBox也会激活并正确显示。
我已经花了几个小时在网上寻找解决方案,却找不到任何东西。我试着从子表中删除除sheet.activate之外的所有内容,发现它什么也没做。我试过将保护方法和隐藏方法帮助程序移动到VBA编辑器的ThisWorkbook部分。我真的不知所措。
有人对我可能做错了什么或导致这种奇怪行为的原因有任何想法吗?任何建议将不胜感激。