I want to set this so that it will open the file in the directory that begins with F-101 and has the highest date of creation.
With Worksheets("F-101").QueryTables.Add(Connection:= _
"TEXT;C:\Program Files\PGAS\F-101_20050623.VOL", Destination:=Worksheets("F-101").Range("A1"))
.Name = "F-100_20050623"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.Refresh BackgroundQuery:=False
End With
by: vhpcompPosted on 2005-06-23 at 19:23:04ID: 14290710
You should be able to find using application file search.
Sub OpenMostRecent()
'the execute method of the file search object returns a 'found files' collection. By sorting by creation date descending and choosing 1 for index, this should give you the newest file.
Dim sFile As String
With Application.FileSearch
.LookIn = "c:\myfolder"
.Filename = "f-101*" 'use asterisk for wild card
.FileType = msoFileTypeExcelWorkbooks 'this file type seems to work for .csv too
.Execute (msoSortByLastModified = msoSortOrderDecsending)
sFile = .FoundFiles(1)
End With
Workbooks.Open sFile
End Sub