之前关闭VBA事件关闭工作簿Cancel = True [英] BeforeClose VBA Event Closing Workbook When Cancel = True
问题描述
我正在尝试编写一个短宏,这将阻止excel工作簿的用户关闭工作簿而不保护第一个工作表。
代码显示消息框,然后继续关闭工作簿。从我的理解,如果取消参数设置为True,工作簿不应该关闭。
Private Sub Workbook_BeforeClose(Cancel As Boolean)
如果表(1).ProtectContents = True Then
取消= False
Else
MsgBox关闭工作簿前请保护独特期货工作表
取消= True
如果
结束Sub
我只需要代码显示消息框,然后如果第一张表不受保护则不关闭。 / p>
如果我将 Application.EnableEvents
设置为假
。在下面的例子中,我已经记得它的状态,把它放回来,但是,我不知道如何到达一个状态的 False
开始。 / p>
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim BlnEventState as Boolean
BlnEventState =应用程序.EnableEvents
Application.EnableEvents = True
如果表格(1).ProtectContents = True然后
取消= False
Else
MsgBox请保护唯一期货工作单关闭工作簿
取消=真
结束如果
Application.EnableEvents = BlnEventState
结束Sub
它可能是一个更安全的长期选项,强制状态,而不是设置它。
Private Sub Workbook_BeforeClose(Cancel As Boolean)
/ pre>
Application.EnableEvents = True
如果表格(1).ProtectContents = True然后
取消= False
Else
MsgBox关闭工作簿前请保护独特期货工作表
取消= True
如果
结束Sub
I'm trying to write a short macro that will prevent the user of an excel workbook from closing the workbook without protecting the first sheet.
The code shows the message box but then proceeds to close the workbook. From my understanding, if the "Cancel" parameter is set to True, the workbook shouldn't close.
Private Sub Workbook_BeforeClose(Cancel As Boolean) If Sheets(1).ProtectContents = True Then Cancel = False Else MsgBox "Please Protect 'Unique Futures' Worksheet Before Closing Workbook" Cancel = True End If End Sub
I just need the code to display the message box and then not close if the first sheet is not protected.
解决方案I could replicate it if I set
Application.EnableEvents
toFalse
. In the below example I have remembered its state to place it back as was after, however, I'm not sure how it gets to a state ofFalse
to begin with.Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim BlnEventState as Boolean BlnEventState = Application.EnableEvents Application.EnableEvents = True If Sheets(1).ProtectContents = True Then Cancel = False Else MsgBox "Please Protect 'Unique Futures' Worksheet Before Closing Workbook" Cancel = True End If Application.EnableEvents = BlnEventState End Sub
It may be a safer long term option to force the state rather then set it back.
Private Sub Workbook_BeforeClose(Cancel As Boolean) Application.EnableEvents = True If Sheets(1).ProtectContents = True Then Cancel = False Else MsgBox "Please Protect 'Unique Futures' Worksheet Before Closing Workbook" Cancel = True End If End Sub
这篇关于之前关闭VBA事件关闭工作簿Cancel = True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!