Open .txt file using wildcard filename

kgartin

Board Regular
Joined
May 6, 2010
Messages
207
Office Version
  1. 365
Platform
  1. Windows
I have a folder with a large number of files inside. Each of them has a filename with a specific, but similar, pattern. I need a code that will look through the files and find a specific file based on the beginning portion of the filename AND a middle portion. Hopefully this will explain it better:

Filename is
FL1-GV-GAINESVILLE-PRESS REPORT----RT1640B-DE1000B1-HALFL1P-158048-28-2024-09-04 124134

I want a macro that will find and open the file based on:
FL1-GV-GAINESVILLE-PRESS REPORT---- * 2024-09-04 * (where *s are wildcards)

Can anyone help with the coding for this?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try the following code...

VBA Code:
Option Explicit

Sub test()

    Dim folderName As String
    Dim fileName As String
    Dim filter As String
    
    folderName = "c:\users\username\desktop\" 'change the path accordingly
    If Right(folderName, 1) <> "\" Then
        folderName = folderName & "\"
    End If
    
    filter = "FL1-GV-GAINESVILLE-PRESS REPORT----*2024-09-04 *.txt" 'removed space before the year
    
    fileName = getFileName(folderName, filter)
    
    If Len(fileName) > 0 Then
        Workbooks.Open folderName & fileName
    Else
        MsgBox "File not found.", vbExclamation
    End If
    
End Sub

Private Function getFileName(ByVal folderName As String, ByVal filter As String) As String

    Dim fileName As String
    
    If Right(folderName, 1) <> "\" Then
        folderName = folderName & "\"
    End If
    
    fileName = Dir(folderName & filter, vbNormal)
    
    If Len(fileName) > 0 Then
        getFileName = fileName
        Exit Function
    End If
    
    getFileName = vbNullString
    
End Function

Hope this helps!
 
Upvote 0
Solution
Try the following code...

VBA Code:
Option Explicit

Sub test()

    Dim folderName As String
    Dim fileName As String
    Dim filter As String
   
    folderName = "c:\users\username\desktop\" 'change the path accordingly
    If Right(folderName, 1) <> "\" Then
        folderName = folderName & "\"
    End If
   
    filter = "FL1-GV-GAINESVILLE-PRESS REPORT----*2024-09-04 *.txt" 'removed space before the year
   
    fileName = getFileName(folderName, filter)
   
    If Len(fileName) > 0 Then
        Workbooks.Open folderName & fileName
    Else
        MsgBox "File not found.", vbExclamation
    End If
   
End Sub

Private Function getFileName(ByVal folderName As String, ByVal filter As String) As String

    Dim fileName As String
   
    If Right(folderName, 1) <> "\" Then
        folderName = folderName & "\"
    End If
   
    fileName = Dir(folderName & filter, vbNormal)
   
    If Len(fileName) > 0 Then
        getFileName = fileName
        Exit Function
    End If
   
    getFileName = vbNullString
   
End Function

Hope this helps!
Perfect thank you!
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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