View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Dick Kusleika[_2_] Dick Kusleika[_2_] is offline
external usenet poster
 
Posts: 66
Default opening a file that will have wildcards in VBA

NHWVBA

You can't use wildcards in Workbooks.Open or Workbooks.OpenText. What if
more than one file has that format? Do you want to open all of them? Just
the first one?

You might look into the Dir function. You can use wildcards with Dir, but
only * and ?, not #. To open the first file that looks like your string

Dim aname as String
Dim bname as String
dim mypath as String

aname = range("A1").Value & "??????.dat"

mypath = "c:\documents" & Format(Int(Now()), "yyyy") & "\lists" &
Format(Int(Now()), "mm-yyyy") & "\"


bname = Dir(mypath & aname)

If Len(baname) = 0 Then
'no file found
Else
Workbooks.OpenText mypath & bname
End If

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com