I do think I am close with what I am attempting
I would like to open the latest modified file based on a partial file name
the partial file name is "WTBY Diff Thursday" ( they usually end in a date ddmmyyy )
all files are .XLS
there usually only 2 to 4 files with this partial name in the folder I am looking through
I think I am close, based on other posts and books that I have looked at
so far "I THINK" am only locating the folder, but since nothing is opening
I am not sure
and I still have to figure out how to open the latest modified file with that partial name
here is what I have so far
I would still need to figure how to find the latest modified file,
and only open that one file
as always that you very much for your help and support
Thomas
I would like to open the latest modified file based on a partial file name
the partial file name is "WTBY Diff Thursday" ( they usually end in a date ddmmyyy )
all files are .XLS
there usually only 2 to 4 files with this partial name in the folder I am looking through
I think I am close, based on other posts and books that I have looked at
so far "I THINK" am only locating the folder, but since nothing is opening
I am not sure
and I still have to figure out how to open the latest modified file with that partial name
here is what I have so far
Code:
Sub openlatestfile()
Dim sStartPath As String
Dim sFileName As String
Dim sFileExt As String
Dim sBOFile As String 'File to open
sFileExt = "*.xls*" 'This is my File Ext- Change to suit
sStartPath = "v:\\repub-file1\Packaging\Waterbury Reports\" 'Location of Folder to look in
'Reads the first file in the directory
sFileName = Dir(sStartPath + sFileExt, vbNormal)
'If the two left characters are "WT" assume as targeted file
If Left(UCase(sFileName), 2) = "WTBY Diff Thursday" Then
sWTFile = sFileName ' NOTE TO SELF.. see the WT in sWTFile
End If
Debug.Print sFileName
'looking thru rest of files in directory
Do While sFileName <> ""
' I THINK....some where in here I need to find the latest file
' of "WTBY Diff Thursday"
sFileName = Dir
'If the two left characters are "WT" then assume as targeted file
If Left(UCase(sFileName), 2) = "WTBY Diff Thursday" Then
sWTFile = sFileName
End If
Debug.Print sFileName
Loop
If sWTFile <> "" Then
MsgBox "Open: " & sWTFile
'Open the file here
Application.Workbooks.Open sStartPath & sWTFile
Else
MsgBox "No file found"
End If
End Sub
and only open that one file
as always that you very much for your help and support
Thomas