Check for file existance

DonAndress

Active Member
Joined
Sep 25, 2011
Messages
362
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello.

I have a code like below:

Code:
'check first file
'
MyPath_contract = "G:\booking\"
    MyClient_contract = "BLK BGF " & contract_name & " Deal Report"
    wbName_contract = MyPath_contract & MyClient_contract & "*.csv"
    
MsgBox (wbName_contract)

If Dir(wbName_contract) = "" Then
    MsgBox ("No 'contract notes' file for " & contract_name & " " & fund_name & " fund." & vbNewLine & "Please save.")
    Exit Sub
End If


'check second file
'
MyPath_appro = "G:\booking\"
    MyClient_appro = "BLK_GIS_" & appro_name & "_Appro_Expro"
    wbName_appro = MyPath_appro & MyClient_appro & "*.csv"

MsgBox (wbName_appro)

If Dir(wbName_appro) = "" Then
    MsgBox ("No 'appro expro' file for " & appro_name & " " & fund_name & " fund." & vbNewLine & "Please save.")
    Exit Sub
End If

And both wbName "MsgBox" print correct paths (asterisks are present).
Then when it comes to testing whether files actually exist I get a run-time error 1004 for the second one (appro).
But in the error message box excel prints the exact name of the file (no asterisk anymore, it prints the full name with the rest of the name in the file) and while I have not defined it as a constant for me it means it found the file in the specified folder.

I double checked the paths, both files exist and I have only file for each name so no confusion with the "*" is possible.
Plus I am sure the same code worked yesterday when I wrote it :)


Can anyone assist on that matter?
How to fix that?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I didn't do an indepth analysis, but it looks like you might be missing a backslash.
Code:
 MyClient_contract = "BLK BGF " & contract_name & " Deal Report"
    wbName_contract = MyPath_contract & MyClient_contract & "*.csv"
Try this:
Code:
 MyClient_contract = "BLK BGF " & contract_name & " Deal Report"
    wbName_contract = MyPath_contract & MyClient_contract & "\*.csv"
 
Upvote 0

Forum statistics

Threads
1,202,984
Messages
6,052,916
Members
444,613
Latest member
questionexcelz

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