With great help from amazing forum community I have VBA script which opens file from relative path which looks like "C:\GENERAL\Handover\2023\7 July\"
But in some cases I can have 2 files inside, for example somebody havent done hygiene in folder
Is it any chance to to add criteria to open last modified file?
But in some cases I can have 2 files inside, for example somebody havent done hygiene in folder
Is it any chance to to add criteria to open last modified file?
VBA Code:
Public Sub Test2()
Dim basePath As String, fullPath As String
Dim yearMonth As String
Dim workbookFileName As String
basePath = "C:\GENERAL\Handover\"
'Current year and month
yearMonth = Format(Date, "yyyy\\m mmmm\\")
fullPath = basePath & yearMonth
If Dir(fullPath, vbDirectory) = vbNullString Or Dir(fullPath & "Handover*.xlsm") = vbNullString Then
'Path doesn't exist or doesn't contain Handover*.xlsm, so construct path for previous month (and year)
yearMonth = Format(DateAdd("m", -1, Date), "yyyy\\m mmmm\\")
fullPath = basePath & yearMonth
End If
workbookFileName = Dir(fullPath & "Handover*.xlsm")
If workbookFileName <> vbNullString Then
Workbooks.Open fullPath & workbookFileName
End If
End Sub