Tweak Error Handling if File Not Found

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I've been working on this particular issue for about 5 hours and cannot seem to find the correct syntax. I have a command button that when clicked, launches a module. That module then calls ~30 other modules; all of which tap out to a shared drive and ingest data from other workbooks. I need to account for the fact that when this is run each month, not every file will be present in the shared drive. I've tried a couple variations of the underlying snippet, but this one seems to get me the closest. If the file is present, the code runs as expected. However, if the file isn't present, it's launching an error dialogue box at
VBA Code:
Set s = Workbooks.Open(fP & fN)
VBA Code:
Dim m As Workbook, s As Workbook
Dim mD As Worksheet, sD As Worksheet
Dim fP As String, fN As String, fE As String
Dim Hdr As Range, c As Range
Dim mDLR As Long, mNLR As Long, sDLR As Long

Set m = ThisWorkbook
Set mD = m.Sheets("New Data")

mDLR = mD.Range("A" & Rows.Count).End(xlUp).Row

fP = "C:\Users\Import Files\"
fN = "LMemo"
fN = Dir(fP & fN & "*.xlsx")

If Len((Dir(fP & fN))) < 1 Then GoTo MissingFile

    Set s = Workbooks.Open(fP & fN) 'Error dialogue box is showing here.  

'My code is here.    
   
MissingFile:
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi
untested but try this update to your code & see if does what you want

VBA Code:
Sub reberryjr()
    Dim m           As Workbook, s      As Workbook
    Dim mD          As Worksheet, sD    As Worksheet
    Dim fP          As String, fN       As String, fE As String
    Dim Hdr         As Range, c         As Range
    Dim mDLR        As Long, mNLR       As Long, sDLR As Long
    
    Set m = ThisWorkbook
    Set mD = m.Sheets("New Data")
    
    mDLR = mD.Range("A" & mD.Rows.Count).End(xlUp).Row
    
    fP = "C:\Users\Import Files\"
    
    fN = fP & "LMemo.xlsx"
    
    On Error GoTo MissingFile
    If Len(Dir(fN, vbDirectory)) > 0 Then
        With Application
            .ScreenUpdating = False: .DisplayAlerts = False
        End With
        
        Set s = Workbooks.Open(fN, 0, False)
        
        'My code is here.
    Else
        'file not found
        Err.Raise 53
        
    End If
    
MissingFile:
    With Application
        .ScreenUpdating = True: .DisplayAlerts = True
    End With
    
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
    
End Sub

Dave
 
Upvote 1
Solution
Hi
untested but try this update to your code & see if does what you want

VBA Code:
Sub reberryjr()
    Dim m           As Workbook, s      As Workbook
    Dim mD          As Worksheet, sD    As Worksheet
    Dim fP          As String, fN       As String, fE As String
    Dim Hdr         As Range, c         As Range
    Dim mDLR        As Long, mNLR       As Long, sDLR As Long
   
    Set m = ThisWorkbook
    Set mD = m.Sheets("New Data")
   
    mDLR = mD.Range("A" & mD.Rows.Count).End(xlUp).Row
   
    fP = "C:\Users\Import Files\"
   
    fN = fP & "LMemo.xlsx"
   
    On Error GoTo MissingFile
    If Len(Dir(fN, vbDirectory)) > 0 Then
        With Application
            .ScreenUpdating = False: .DisplayAlerts = False
        End With
       
        Set s = Workbooks.Open(fN, 0, False)
       
        'My code is here.
    Else
        'file not found
        Err.Raise 53
       
    End If
   
MissingFile:
    With Application
        .ScreenUpdating = True: .DisplayAlerts = True
    End With
   
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
   
End Sub

Dave
@dmt32 this worked on one of my import subs. Before I implement it on the others, what does Err.Raise 53 do/mean? I've not seen that before.
 
Upvote 0
@dmt32 this worked on one of my import subs. Before I implement it on the others, what does Err.Raise 53 do/mean? I've not seen that before.

I have added an error handling routine to your code.
By Enabling an error-handling routine and specifying the location to send the error(s) this should manage all errors in your code.

Where a file is not found by the Dir function I raised the error myself using the Raise Method
this action sends the code to your label & displays the msgbox with the specified error. Such and approach is preferable to using the Goto statement on its own which, outside use in error handlers, is widely regarded as a poor programming practice and should be avoided.

Dave
 
Upvote 0
I have added an error handling routine to your code.
By Enabling an error-handling routine and specifying the location to send the error(s) this should manage all errors in your code.

Where a file is not found by the Dir function I raised the error myself using the Raise Method
this action sends the code to your label & displays the msgbox with the specified error. Such and approach is preferable to using the Goto statement on its own which, outside use in error handlers, is widely regarded as a poor programming practice and should be avoided.

Dave
I've been tasked with some more complicated things than what I'm used to, so I'm going to have to look into error handling deeper. I appreciate your assistance!
 
Upvote 0
I've been tasked with some more complicated things than what I'm used to, so I'm going to have to look into error handling deeper. I appreciate your assistance!

you are welcome glad suggestion helps
There are plenty of sites like this one Error Handling that provide useful guidance.

Dave
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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