View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default open workbook using wildcard

You could loop through the files in a specified folder and use the like
operator to find the one you want. You'd need to change the Path constant to
whatever you need. You could also prompt the user for the filename using
GetOpenFileName method (might be better choice if the path is subject to
change or there could be multiple filenames that match your criteria). This
example only opens the first file it finds that matches the criteria.


Sub test()
Const Path = "C:\temp\test"
Dim FName As String
Dim FSO As Object
Dim Folder As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder(Path)
FName = Range("SourceFile").Value

For Each x In Folder.Files
If UCase(x.Name) Like UCase(FName & "*" & ".xls") Then
Workbooks.Open (Path & Application.PathSeparator _
& x.Name)
Exit For
End If
Next x

End Sub


"Ben" wrote:

Hello, I'd like a line of code to open a workbook whose middle characters
change daily. The first 4 characters remain constant. Let's say the first
four characters of the file are "data" and this info is in a cell named
"sourcefile". Can I use wildcards for the rest of the filename with
WORKBOOKS.OPEN FILENAME
Thank you