VBA InputBox提示两次
我有一个简单的脚本在closures工作簿之前需要密码 (以防止意外closures),但是如果input正确的关键字,则InputBox
重新打开。 我已经创build了下面的脚本的多个迭代,但我无法解决它。
Sub Workbook_BeforeClose(Cancel As Boolean) If InputBox("Please enter the password to close the workbook.") <> "pa55word" Then MsgBox ("Incorrect password. Please try again") Cancel = True Exit Sub Else GoTo GoToClose End If GoToClose: ThisWorkbook.Close SaveChanges:=False End Sub
如果你这样编码:
- 代码只需简单地继续保存工作簿,如果密码正确无需第二次closures
-
ThisWorkbook.Saved
在前面告诉Excel的工作簿是完全更新所以不会是一个你想要保存消息 – 即它执行与ThisWorkbook.Close SaveChanges:=False
的False
部分相同的任务。closuresThisWorkbook.Close SaveChanges:=False
和现有的保存事件closures工作簿。
重新切割
Sub Workbook_BeforeClose(Cancel As Boolean) ThisWorkbook.Saved = True If InputBox("Please enter the password to close the workbook.") <> "pa55word" Then MsgBox ("Incorrect password. Please try again") Cancel = True End If End Sub
closures前禁用事件。