在文件不可用时取消显示错误消息框

问题描述 投票:0回答:1

我有一个代码可以根据今天的日期打开另一个工作簿。我计划使用任务计划程序自动运行,但是在找不到外部工作簿的情况下,我希望我的工作簿自动关闭,以免打开时卡住。但是我遇到错误消息“很抱歉,我们无法打开'D:\ Users .......“”。无论如何都禁止显示此错误消息?

Sub DataGrab()
    On Error GoTo ErrorHandler

    Dim extwbk As Workbook, twb As Workbook

    Set twb = ThisWorkbook
    Set extwbk = Workbooks.Open("D:\Users\Desktop\Report " & Format(Now, "DD-MMM-YYYY") & ".xls*", UpdateLinks:=0)
    '
    'my code
    '   
    Exit Sub

ErrorHandler:
    Application.DisplayAlerts = False
    Application.Quit
    Application.DisplayAlerts = True

End Sub
excel vba
1个回答
0
投票

您必须首先检查工作簿extwbk是否存在

© www.soinside.com 2019 - 2024. All rights reserved.