Open last modified file from relative path

Josu

New Member
Joined
Mar 2, 2021
Messages
39
Office Version
  1. 2010
Platform
  1. Windows
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?
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
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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?

Add this function:
VBA Code:
Private Function Find_Latest_File(ByVal folder As String, Optional matchFiles As String = "*.*") As String

    Dim fileName As String
    Dim fileTime As Date, latestTime As Date
    
    If Right(folder, 1) <> "\" Then folder = folder & "\"
    
    Find_Latest_File = ""
    fileTime = 0
    fileName = Dir(folder & matchFiles)
    While fileName <> vbNullString
        fileTime = FileDateTime(folder & fileName)
        If fileTime > latestTime Then
            Find_Latest_File = folder & fileName
            latestTime = fileTime
        End If
        fileName = Dir
    Wend

End Function
and call it thus:
VBA Code:
    workbookFileName = Find_Latest_File(fullPath, "Handover*.xlsm")
    If workbookFileName <> "" Then
        Workbooks.Open workbookFileName
    End If
 
Upvote 1
Basically looks like this - and works like a charm. Thanks a lot!
Need to start learning VBA. would be very helpfull for my work

VBA Code:
Public Sub Test2()
   
    Dim basePath As String, fullPath As String
    Dim yearMonth As String
    Dim workbookFileName As String
   
    basePath = "C:\GENERAL\"
   
    '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 = Find_Latest_File(fullPath, "Handover*.xlsm")
    If workbookFileName <> "" Then
        Workbooks.Open workbookFileName
    End If
   


End Sub

Private Function Find_Latest_File(ByVal folder As String, Optional matchFiles As String = "*.*") As String

    Dim fileName As String
    Dim fileTime As Date, latestTime As Date
    
    If Right(folder, 1) <> "\" Then folder = folder & "\"
    
    Find_Latest_File = ""
    fileTime = 0
    fileName = Dir(folder & matchFiles)
    While fileName <> vbNullString
        fileTime = FileDateTime(folder & fileName)
        If fileTime > latestTime Then
            Find_Latest_File = folder & fileName
            latestTime = fileTime
        End If
        fileName = Dir
    Wend

End Function
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top