VBA - IF Run Time Error 1004 then show Userform + exit

Alphacsulb

Active Member
Joined
Mar 20, 2008
Messages
414
When the file name doesn't exist then I get a run time error 1004. Which I know means that the file doesn't exist. However, I want to show/create a userform that tells other people using this worksheet the reason why it didn't work so that they can correct it themselves instead of asking me every time.

This is the code that looks for the file name in I2, in the folder Reports

Code:
ChDir "M:\Reports" 'Where to start looking in
    Workbooks.Open Filename:="M:\Reports\" & ActiveSheet.Range("I2") ' Opens the Billing file, the file name is in I2

What I would like to do is add the following:
IF the file does not exist (creates the run time error 1004) then
ReasonForError.Show
Exit Sub

Would this go in front or before my current piece of code?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi,

Maybe something like this:

Code:
    On Error GoTo Finish
    
    ChDir "M:\Reports" 'Where to start looking in
        Workbooks.Open Filename:="M:\Reports\" & ActiveSheet.Range("I2") ' Opens the Billing file, the file name is in I2
    
Finish:
    
    If Error.Number <> 0 Then
        ReasonForError.Show
        Exit Sub
    End If
 
Upvote 0
Or If your handling other errors and you only want this to show for error 1004 then maybe this:

Code:
    On Error GoTo Finish
    
    ChDir "M:\Reports" 'Where to start looking in
        Workbooks.Open Filename:="M:\Reports\" & ActiveSheet.Range("I2") ' Opens the Billing file, the file name is in I2
    
Finish:
    
    If Error.Number = 1004 Then
        ReasonForError.Show
        Exit Sub
    End If
 
Upvote 0
Try like this. You doin't need ChDir

Code:
If Dir("M:\Reports\" & ActiveSheet.Range("I2")) = "" Then
    MsgBox "File doesn't exist", vbexclamation
    Exit Sub
Else
    Workbooks.Open Filename:="M:\Reports\" & ActiveSheet.Range("I2") ' Opens the Billing file, the file name is in I2
End If
 
Upvote 0
Both versions worked. I ended up using MsgBox, as it saved the step of creating the UserForm.
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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