VBA - Open most recent file in folder

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
Trying to create a VBA that opens the most recent file in folder based on name. The folder would have files in this format (seen below).. I would want it to open "FILENAME_2022-10-20 - (Published On 2022-10-20 10-09-16)". the issue is getting around the "(Published On....".

FILENAME_2022-10-20 - (Published On 2022-10-20 10-09-16)
FILENAME_2022-10-19 - (Published On 2022-10-19 09-04-55)
FILENAME_2022-10-18 - (Published On 2022-10-18 09-16-22)
FILENAME_2022-10-17 - (Published On 2022-10-17 09-17-49)
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this:
The macro asks you to select the folder with the files.

VBA Code:
Sub open_most_recent_file()
  Dim sPath As String, sFile As String, theFile As String, psh As String
  Dim xMax As Double, sDate As Date, n As Long

  With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select Folder"
    .AllowMultiSelect = False
    If .Show <> -1 Then Exit Sub
    sPath = .SelectedItems(1) & "\"
  End With
  
  sFile = Dir(sPath & "*.*")
  Do While sFile <> ""
    n = InStr(1, sFile, "Published On")
    If n > 0 Then
      psh = Mid(sFile, n + 13, 19)
      sDate = CDate(Left(psh, 10) & " " & Replace(Right(psh, 8), "-", ":"))
      If sDate > xMax Then
        theFile = sFile
        xMax = sDate
      End If
    End If
  
    sFile = Dir()
  Loop
  
  If theFile <> "" Then
    Workbooks.Open sPath & theFile
  End If
End Sub
 
Upvote 0
Try this:
The macro asks you to select the folder with the files.

VBA Code:
Sub open_most_recent_file()
  Dim sPath As String, sFile As String, theFile As String, psh As String
  Dim xMax As Double, sDate As Date, n As Long

  With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select Folder"
    .AllowMultiSelect = False
    If .Show <> -1 Then Exit Sub
    sPath = .SelectedItems(1) & "\"
  End With
 
  sFile = Dir(sPath & "*.*")
  Do While sFile <> ""
    n = InStr(1, sFile, "Published On")
    If n > 0 Then
      psh = Mid(sFile, n + 13, 19)
      sDate = CDate(Left(psh, 10) & " " & Replace(Right(psh, 8), "-", ":"))
      If sDate > xMax Then
        theFile = sFile
        xMax = sDate
      End If
    End If
 
    sFile = Dir()
  Loop
 
  If theFile <> "" Then
    Workbooks.Open sPath & theFile
  End If
End Sub
Instead of selecting the folder, can you include one where I can insert the folder path where all the files are located?
 
Upvote 0
can you include one where I can insert the folder path where all the files are located?
Sure, try this:

Rich (BB code):
Sub open_most_recent_file()
  Dim sPath As String, sFile As String, theFile As String, psh As String
  Dim xMax As Double, sDate As Date, n As Long

  sPath = "c:\work\files\"
  
  sFile = Dir(sPath & "*.*")
  Do While sFile <> ""
    n = InStr(1, sFile, "Published On")
    If n > 0 Then
      psh = Mid(sFile, n + 13, 19)
      sDate = CDate(Left(psh, 10) & " " & Replace(Right(psh, 8), "-", ":"))
      If sDate > xMax Then
        theFile = sFile
        xMax = sDate
      End If
    End If
  
    sFile = Dir()
  Loop
  
  If theFile <> "" Then
    Workbooks.Open sPath & theFile
  End If
End Sub
 
Upvote 0
Sure, try this:

Rich (BB code):
Sub open_most_recent_file()
  Dim sPath As String, sFile As String, theFile As String, psh As String
  Dim xMax As Double, sDate As Date, n As Long

  sPath = "c:\work\files\"
 
  sFile = Dir(sPath & "*.*")
  Do While sFile <> ""
    n = InStr(1, sFile, "Published On")
    If n > 0 Then
      psh = Mid(sFile, n + 13, 19)
      sDate = CDate(Left(psh, 10) & " " & Replace(Right(psh, 8), "-", ":"))
      If sDate > xMax Then
        theFile = sFile
        xMax = sDate
      End If
    End If
 
    sFile = Dir()
  Loop
 
  If theFile <> "" Then
    Workbooks.Open sPath & theFile
  End If
End Sub
Apologies, but the files names are;

FILENAME_2022-10-20
FILENAME_2022-10-19
FILENAME_2022-10-18
FILENAME_2022-10-17

Can you please update the code so that now it doesn't include anything right of the date? it works perfectly for the initial request.
 
Upvote 0
Now I don't understand what you need anymore.
Could you explain again what you need?

Each file has a creation date, last modification date and in the name of your files you have another date.
What do you want to do, you must be more specific, to understand what you want.

By the way, are they excel files?
 
Upvote 0
Now I don't understand what you need anymore.
Could you explain again what you need?

Each file has a creation date, last modification date and in the name of your files you have another date.
What do you want to do, you must be more specific, to understand what you want.

By the way, are they excel files?
Yes sorry, i can provide more description.

Trying to create a VBA that opens the most recent file in folder based on name. All files are in .xlsx format. An example of the folder with the file name is shown below;
FILENAME_2022-10-20
FILENAME_2022-10-19
FILENAME_2022-10-18
FILENAME_2022-10-17

is there anything else?
 
Upvote 0
Try this:

VBA Code:
Sub open_most_recent_file()
  Dim sPath As String, sFile As String, theFile As String
  Dim xMax As Double, sDate As Date

  sPath = "c:\trabajo\files\"
  
  sFile = Dir(sPath & "*.*")
  Do While sFile <> ""
    If UCase(Left(sFile, 9)) = "FILENAME_" Then
      sDate = CDate(Mid(sFile, 10, 10))
      If sDate > xMax Then
        theFile = sFile
        xMax = sDate
      End If
    End If
  
    sFile = Dir()
  Loop
  
  If theFile <> "" Then
    Workbooks.Open sPath & theFile
  End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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