【问题标题】:VBA InputBox Prompts TwiceVBA InputBox 提示两次
【发布时间】:2015-09-02 20:43:54
【问题描述】:

我有一个简单的脚本,要求在关闭工作簿之前输入 密码(以防止意外关闭),但如果我输入正确的关键字,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

【问题讨论】:

    标签: excel vba prompt inputbox


    【解决方案1】:

    如果你这样编码:

    • 如果密码正确而无需再次关闭,代码将继续保存工作簿
    • 前面的 ThisWorkbook.Saved 告诉 Excel 工作簿已完全更新,因此不会有 您要保存 消息 - 即它与 @987654323 的 False 部分执行相同的任务@ 并且现有的 Save 事件关闭工作簿。

    重新剪辑

    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
    

    【讨论】:

    • 感谢您的建议!我没想过要包含 .Saved 部分。
    【解决方案2】:

    在关闭前禁用事件。

    【讨论】:

    • 我曾尝试使用Application.EnableEvents = False 执行此操作,但是当我重新打开文档时,我的宏执行 none。我相信这只发生在我关闭单个工作簿而不是整个 Excel 实例时,但我想知道是否有更好的解决方案。
    猜你喜欢
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2018-01-08
    • 1970-01-01
    相关资源
    最近更新 更多