在引用和打开外部xlsm库时强制退出

时间:2017-01-24 01:00:20

标签: vba excel-vba excel

我有三个文件,一个是app.xlsm,另一个是lib.xlsm,而app.xlsm使用lib.xlsm作为参考(在工具中指定 - >参考资料)。第三个,third.xlsm具有以下代码:

Private Sub Workbook_Open()
    prompt = MsgBox("If you click Ok, Excel will force close." & vbCrLf & _
            "If you click Cancel, you can work with the file", vbOKCancel)
    If prompt = vbOK Then
        Application.DisplayAlerts = False
        thisworkbook.Close True
        Application.Quit
    End If
End Sub

假设我已打开applib。现在,当我打开third,然后在提示符处单击“确定”时,app将关闭,但lib仍处于打开状态。即Excel不会强行关闭。

我需要Excel关闭所有文件而不保存它们并静默关闭。

非常感谢。

1 个答案:

答案 0 :(得分:1)

如果您在此实例中循环打开工作簿,即

,会发生什么
voting_status = a

用于关闭所有Excel实例的热核选项

Private Sub Workbook_Open()
    Dim wb As Workbook
    prompt = MsgBox("If you click Ok, Excel will force close." & vbCrLf & _
            "If you click Cancel, you can work with the file", vbOKCancel)

    If prompt = vbOK Then
       For Each wb In Application.Workbooks
         If wb.Name <> ThisWorkbook.Name Then wb.Close False
       Next
    End If
    ThisWorkbook.Saved = True
    Application.Quit
End Sub
相关问题