open file with wildcard name

smakatura

Board Regular
Joined
May 27, 2011
Messages
141
I have a file 09-08-11 tmp.csv in a folder
  • the file names will change (date wise) so the file name with a wildcard would be *tmp.csv
    • for future reference the *tmp.csv file is the variable tmpfilename
  • there may not be a tmpfilename file in the folder
  • there will never be more than one tmpfilename
  • the location(directory) of the tmpfilename will remain the same at all times
I want to
  • search the directory location
  • see if a tmpfilename exists
  • if it does open the tmpfilename that is in the folder
    • keeping in mind that the actual file name will change as a new one is created on a new date.
I can get the macro to searh to see if tmpfilename exists...but I can't get it to open it if it does. (so the workbooks.open part is what is not working).

below is my partial code.
HTML:
    Dim tmpfilename As String
 
    tmpfilename = "*tmp.csv"
 
    If Len(Dir("C:\Users\smakatura\Documents\call report\" & tmpfilename,
            vbDirectory)) <> 0 Then
 
        Workbooks.Open filename:="C:\Users\smakatura\Documents\call report\" & tmpfilename

I am thinking that somehow I need to assign the actual file name (09-08-11 tmp.csv) to a new variable (we will call it actualtmpfilename) and then have it open the actualtmpfilename document. but not sure how to assign that actual file name to the new variable.

I know I have not added actualfilename as a variable above yet.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi

You are using DIR with the vbDirectory attribute but are searching for a file, not a folder. This would cause a problem.

Try like this:

Code:
Dim strMyFile As String

strMyFile = Dir(Path & "*tmp.csv")

If strMyFile<>"" Then 
  Set wb = Workbook.sOpen(Path & "strMyFile")
  'more code...
Else
  Msgbox "File Not found!"
End if
 
Upvote 0
thanks that worked.

the code had one typo of workbook.sopen instead of workbooks.open but typing errors occur.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,896
Members
452,948
Latest member
Dupuhini

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