On Error GoTo

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
I'm trying to use an "On Error" for the first time to install some error handling. Basically, I want to open a file in a specific folder only if it has the current day's date in it, otherwise, i want to exit the sub and call upon another procedure within the module without displaying to me that the file doesn't exist in the location. I've figured out how to do this, but only by putting Application.DisplayAlerts around the open file line and I feel like I shouldn't have to do that with this code. Is this the only way, or am I missing something?

Code:
On Error GoTo ErrHandler
    Application.DisplayAlerts = False
    Workbooks.Open Filename:="...\FHR to SUB\FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx"
    Application.DisplayAlerts = True
    Range("B6").Formula = "='[FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx]Summary'!$D$31"
    Range("B7").Formula = "=B6-'[FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx]Summary'!$D$36-'[FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx]Summary'!$D$47"
    Workbooks("FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx").Close False
    Call EDCSUBtoCUS
    Exit Sub
ErrHandler:
    Call EDCSUBtoCUS
    Exit Sub

If i don't use the displayalerts line, it tells me the file is not in the specified location. I'm trying to avoid that alert so it just continues to run.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
It would be better to check if the file exists before trying to open it, you can avoid the error that way.
The FileExists function at the bottom of the code uses DIR to check if the file exists.

Code:
Sub Test()
    Dim sFilePath As String
    Dim wrkBk As Workbook

    sFilePath = "...\FHR to SUB\FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx"
    If FileExists(sFilePath) Then
        Set wrkBk = Workbooks.Open(sFilePath)
        With wrkBk.Worksheets("Sheet1")
            .Cells(6, 2).Formula = "='[FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx]Summary'!$D$31"
            .Cells(7, 2).Formula = "=B6-'[FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx]Summary'!$D$36-'[FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx]Summary'!$D$47"
        End With
        Workbooks("FHR to SUB " & Format(Date, "yyyymmdd") & " - EDC.xlsx").Close False
    End If
    Call EDCSUBtoCUS
End Sub

Public Function FileExists(ByVal DirectoryAndFileName) As Boolean
'// Will return True if file/folder Exists
    FileExists = (Len(Dir(DirectoryAndFileName, vbDirectory + vbHidden + vbSystem)) > 0)
End Function
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top