Get file address without opening the file?

xceej

New Member
Joined
Jun 21, 2018
Messages
10
Hi
I know it is possible to open a file with using wildcards, but is it possible to get the full address/filename using wildcards without opening the file?
eg opening using wildcards:

VBA Code:
FilePath=FolderPath & "*DUB*"
Workbooks.Open FilePath

And I know you can get full address/name with:

VBA Code:
ThisWorkbook.FullName

The reason is that I am pulling data from files that will be changed monthly, and only part of the name will remain consistent. I'm using ADO functions to pull in the data without opening the sheets, but for that I need to point specifically to the workbook, wildcards don't work.

Thanks for any help
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
The following example uses the Dir function to search for the first file in the specified folder that matches your criteria, and then assigns the file name to the variable filename. If no file is found, a message is displayed, and then it exits the sub. Otherwise, it continues with the rest of the code.

VBA Code:
    Dim filename As String
    filename = Dir(FolderPath & "*DUB*", vbNormal)
   
    If Len(filename) = 0 Then
        MsgBox "No file found!", vbExclamation
        Exit Sub
    End If
   
    'etc
    '
    '

Hope this helps!
 
Upvote 0
Solution
Here is a little example showing you how you can loop through a whole directory, and capture all file names that have a certain word in them, and set the full name equal to a variable (I return it as a MsgBox for demonstration purposes).
VBA Code:
Sub MyTest()

    Dim myPath As String
    Dim myFile
    Dim myFullName As String

    myPath = "C:\Temp\"

    myFile = Dir(myPath & "*DUB*")

    Do While myFile <> ""
        myFullName = myPath & myFile
        MsgBox myFullName
        
        myFile = Dir
    Loop
    
End Sub
Change your file path to suit.
 
Upvote 0
The following example uses the Dir function to search for the first file in the specified folder that matches your criteria, and then assigns the file name to the variable filename. If no file is found, a message is displayed, and then it exits the sub. Otherwise, it continues with the rest of the code.

VBA Code:
    Dim filename As String
    filename = Dir(FolderPath & "*DUB*", vbNormal)
  
    If Len(filename) = 0 Then
        MsgBox "No file found!", vbExclamation
        Exit Sub
    End If
  
    'etc
    '
    '

Hope this helps!
That's perfect. Then I can combine FolderPath & Filename and works in the subs down the line. Thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,214,845
Messages
6,121,902
Members
449,053
Latest member
Guy Boot

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