If file doesn't exist exit vba

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi,

I have the following code where it opens a file and then copies sheets etc.:

VBA Code:
Sub CopyComparison()

    Application.DisplayAlerts = False
   
    Dim filename As String
    filename = ThisWorkbook.Path & Application.PathSeparator & "Scheduler_Reports v2.xlsx"
   
    Dim wk As Workbook
    Set wk = Workbooks.Open(filename, ReadOnly:=True)

'code

End Sub

Because Scheduler Reports v2 will not be created until later in the month, can someone help with the code to exit this macro if the file is not there but if it is continue on?

I tried this thread but couldn't seem to get it to work


Thank you in advance for your assistance!
 

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.
Try this:

VBA Code:
Sub CopyComparison()

    Application.DisplayAlerts = False
   
    Dim wk As Workbook
    Dim filename As String
    
    filename = ThisWorkbook.Path & Application.PathSeparator & "Scheduler_Reports v2.xlsx"
       
    If Dir(filename) = "" Then
      MsgBox "File does not exists"
      Exit Sub
    End If
    Set wk = Workbooks.Open(filename, ReadOnly:=True)

'code

End Sub
 
Upvote 0
Solution
VBA Code:
If Len(Dir(filename))=0 then exit sub
 
Upvote 0
Hi,
one way maybe

VBA Code:
Sub CopyComparison()
    Dim wk       As Workbook
    Dim filename As String
    
    filename = ThisWorkbook.Path & Application.PathSeparator & "Scheduler_Reports v2.xlsx"
   
   If Not Dir(filename, vbDirectory) = vbNullString Then
    
        Set wk = Workbooks.Open(filename, ReadOnly:=True)

'code
    Else
    
        MsgBox filename & Chr(10) & "File Not Found", 48, "Not Found"
    
    End If
End Sub

Dave
 
Upvote 0
Thank you all for the help and suggestions! I got all three to work but I did have to put filename in "" for it to work.

Thank you all again for your help, it is much appreciated!!!
 
Upvote 0
Oops spoke too soon.

I just tried running a test when it does exist and it's still saying that the file does not exist. Can you help with that?

Thanks again!
 
Upvote 0
Remove the quotes around filename.
It is a variable name, not a literal string.
 
Upvote 0
Without the quotes around filename it runs into an error on the "If Dir(filename) = "" Then" line even when there is no file.
 
Upvote 0
This means that the filename path is wrong and does not comply with the standards.
What is the value of filename at the moment of error.
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,457
Members
449,161
Latest member
NHOJ

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