Search For File In Directory Using Wildcard Works, But Now To Get The Complete Name Of That File

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
This code checks to see if a file (tar_str) exists in a directory.

Code:
Sub cheese()
    

    sdate = 43609 'May 24 2019
    EDate = 43757 'Oct 20 2019
    Days = 148
    For lp = sdate To EDate
        t_mon = Format(lp, "mmm")
        t_day = Day(lp)
        t_dayy = Format(lp, "ddd")
        tar_str = t_mon & "-" & t_day & " (" & t_dayy & ") Schedule_*"
        srcfile = tar_str
        
        strFileName = srcpath & srcfile
        strFileExists = Dir(strFileName)
 
        If strFileExists = "" Then
            MsgBox "The selected file doesn't exist"
        Else
            MsgBox "The selected file exists"
        End If
        lp = lp + 1
    Next lp
     
End Sub

"tar_str" consists of a trailing wildcard as it will locate the one file (if it exists) in the directory that is of the flavour "_3.xlsx", "_5.xlsx" or "_6.xlsx".

So, now that such a file is determined to exist, how can I get that file's full name (to include the portion represented by the asterisk)? I need to now open it, and I can't open a workbook with a wildcard.
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
strFileExists is the actual file name returned by Dir, so you need to prepend the path to open it:
Code:
        strFileExists = Dir(strFileName)
 
        If strFileExists = "" Then
            MsgBox "The selected file doesn't exist"
        Else
            MsgBox "The selected file exists"
            Workbooks.Open srcpath & strFileExists
        End If
 
Upvote 0
Just a quick look.
I think the wildcard as used will return the Schedule_* and not a filename.
I could be wrong. srcpath isn't defined

Code:
tar_str = Dir(srcpath & t_mon & "-" & t_day & " (" & t_dayy & ") Schedule_*")
 
Last edited:
Upvote 0
Yes, OP you need to define the srcpath string, assuming it isn't a global variable defined elsewhere. I advise you to always have Option Explicit at the top of modules.

Dir returns a complete file name.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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