Opening a file using wildcards

60john

Board Regular
Joined
Apr 25, 2013
Messages
80
Hi there,

I would like some help with opening files with variable file names.

A sample file name would be: "C:\abcd10062015defgh.csv"

PHP:
Dim file_path1 As String
        Dim MyDate As String

file_path1 = "C:\ABCD" & MyDate & "defgh.csv"
    
    If Dir(file_path1) <> "" Then
        Workbooks.Open file_path1

The question is how do I use wildcards to open this file having gotten the date as a variable. The characters before and after the DDMMYYYY are never the same

Thanks in advance.
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Perhaps.
Code:
Dim file_path1 As String
Dim MyDate As String

    MyDate = Format(Date, "ddmmyyyy")

    file_path1 = "C:\*" & MyDate & "*.csv"
    
    If Dir(file_path1) <> "" Then
        Workbooks.Open "C:\" & file_path1 
        ' other code
    Else
        MsgBox "File not found.
    End If
 
Upvote 0
Perhaps.
Code:
Dim file_path1 As String
Dim MyDate As String

    MyDate = Format(Date, "ddmmyyyy")

    file_path1 = "C:\*" & MyDate & "*.csv"
    
    If Dir(file_path1) <> "" Then
        Workbooks.Open "C:\" & file_path1 
        ' other code
    Else
        MsgBox "File not found.
    End If

Thanks for the prompt reply.

Unfortunately I get a error message that says " 'c:\*160615*.csv could not be found"
 
Upvote 0
Try this.
Code:
Dim file_path1 As String
Dim MyDate As String

    MyDate = Format(Date, "ddmmyyyy")

    file_path1 = Dir("C:\*" & MyDate & "*.csv")
    
    If file_path1 <> "" Then
        Workbooks.Open "C:\" & file_path1 
        ' other code
    Else
        MsgBox "File not found.
    End If
 
Upvote 0
Try this.
Code:
Dim file_path1 As String
Dim MyDate As String

    MyDate = Format(Date, "ddmmyyyy")

    file_path1 = Dir("C:\*" & MyDate & "*.csv")
    
    If file_path1 <> "" Then
        Workbooks.Open "C:\" & file_path1 
        ' other code
    Else
        MsgBox "File not found.
    End If


That worked perfectly Norie.

Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,203,515
Messages
6,055,848
Members
444,828
Latest member
StaffordStag

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