Hello. I have the code shown below (ripped no doubt from someone on this board) to open the newest file inside a directory. I need to be able to check the partial filename to make sure the correct file was put in the correct folder before opening. If the file is the wrong one, I'd like a msgbox to inform me of this.
In this case, a downloaded file always has this begining "starburst-"...
In short, throw a msgbox if the newest file in the folder doesn't start with "starburst-"
Thanks for looking/advising. I'd be lost without this resource.
In this case, a downloaded file always has this begining "starburst-"...
VBA Code:
Sub StarburstNewestFile()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Application.DisplayAlerts = False
Application.ScreenUpdating = False
' Dim path As String
MyPath = ThisWorkbook.path & "\Exports\StarburstExport\"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.csv", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
End Sub
In short, throw a msgbox if the newest file in the folder doesn't start with "starburst-"
Thanks for looking/advising. I'd be lost without this resource.