VBA InputBox 提示两次
VBA InputBox Prompts Twice
我有一个简单的脚本要求在关闭工作簿之前输入密码(以防止意外关闭),但是如果我输入正确的 InputBox
会重新打开关键词。我已经创建了以下脚本的多个迭代,但我无法解决它。
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
关闭前禁用事件。
如果你这样编码:
- 如果密码正确,代码将继续保存工作簿而无需第二次关闭
- 前面的
ThisWorkbook.Saved
告诉 Excel 工作簿已完全更新,因此不会有 do you want to save 消息 - 即它执行与 ThisWorkbook.Close SaveChanges:=False
的 False
部分相同的任务,现有的保存事件关闭工作簿。
重新剪辑
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
我有一个简单的脚本要求在关闭工作簿之前输入密码(以防止意外关闭),但是如果我输入正确的 InputBox
会重新打开关键词。我已经创建了以下脚本的多个迭代,但我无法解决它。
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
关闭前禁用事件。
如果你这样编码:
- 如果密码正确,代码将继续保存工作簿而无需第二次关闭
- 前面的
ThisWorkbook.Saved
告诉 Excel 工作簿已完全更新,因此不会有 do you want to save 消息 - 即它执行与ThisWorkbook.Close SaveChanges:=False
的False
部分相同的任务,现有的保存事件关闭工作簿。
重新剪辑
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