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!
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You cannot use Dir with online files.
Btw, to see a variable's value in debug mode go to the immediate window and type:
?filename
then press enter.
 
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!

If Dir Function not suitable for your requirement then try following as an alternative approach & see if any help

VBA Code:
Sub CopyComparison()
    Dim wk              As Workbook
    Dim filename        As String
    
    filename = ThisWorkbook.Path & Application.PathSeparator & "Scheduler_Reports v2.xlsx"

    On Error GoTo myerror
    
    Set wk = Workbooks.Open(filename, ReadOnly:=True)
        
        'code

myerror:
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Dave
 
Upvote 0
if you have an online file, the separator that should be used is "/". If you are on Windows then Application.PathSeparator is "\".
So you will get the same error as before. So you have to hardcode the correct path separator.
And I would write the code from dmt32 like this:
VBA Code:
Sub CopyComparison()
    Dim wk              As Workbook
    Dim filename        As String
   
    filename = ThisWorkbook.Path & "/" & "Scheduler_Reports v2.xlsx"

    On Error Resume next
    Set wk = Workbooks.Open(filename, ReadOnly:=True)
    doevents
    if wk is nothing then exit sub
    on error goto 0

........ the rest of your code goes here

End Sub
 
Upvote 0
dmt32/bobsan42 - Thank you very much for this solution! This worked with the sharepoint constraint I have. Oddly enough the filename had / within the website portion but just before Scheduler_Reports v2 it was a \.

I marked DanteAmor's answer as the solution as that seems more likely to be the solution for others (if that goes against mrexcel etiquette, please let me know and I will change the solution) but the answers you both provided are definitely the answers when a sharepoint site is involved.

Thank you again for your help here and have a great weekend!
 
Upvote 0
dmt32/bobsan42 - Thank you very much for this solution! This worked with the sharepoint constraint I have. Oddly enough the filename had / within the website portion but just before Scheduler_Reports v2 it was a \.

I marked DanteAmor's answer as the solution as that seems more likely to be the solution for others (if that goes against mrexcel etiquette, please let me know and I will change the solution) but the answers you both provided are definitely the answers when a sharepoint site is involved.

Thank you again for your help here and have a great weekend!
Glad we could help.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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