0
votes

I have a code that open another workbook based on today date. I plan to use task scheduler to auto run but in the case of the external workbook not found, I want my workbook to auto close so it wouldnt stuck there opened. But i encounter error message `Sorry we couldn't open 'D:\Users......."'. Anyway to suppress this error message?

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
1
You have to turn off DisplayAlerts before the problem might happen, not in the error handling code. By then it's too late. It also makes no sense to turn DisplayAlerts back on after you quit the application.Ken White
Dont try to suppress this error, instead check to see if the file exists first using Dir()braX

1 Answers

0
votes

You have to first check if the workbook extwbk exists

you can do so with fso.fileexists function

if the file does not exists you can go to errorhandler

please check this image with references I used.

enter image description here