4rth most recent file from the file name date

torvaia

New Member
Joined
Feb 22, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi ,
I have a folder with different files, updated every week. One file is added per week.
I need a VBA code which take the name of the most recent file and the 4rth one based on the date founded in the file name in the format "YYYY-Month-DD Targets - ALL.xls", eg. "2023-Apr-24 BDD Targets - ALL.xls".

Could someone help? This below part of my code..
VBA Code:
For Each objfile In myFolder.Files
                    
            If InStr(1, objfile.Name, ".xls") > 0 Then
                    i = i + 1
                    fileName = objfile.Name
                    dateParts = Split(fileName, "-")
                    yearPart = dateParts(0)
                    monthPart = Month(DateValue(dateParts(1) & " 1, 2021")) ' Convert month name to number
                    dayPart = Left(dateParts(2), 2)
                    dateStr = DateSerial(yearPart, monthPart, dayPart)


Thanks a lot.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this:
VBA Code:
Public Sub Most_Recent_Files_In_Folder()

    Dim folderPath As String, fileName As String
    Dim filesArray As Object
    Dim fileNameDate As Date
    
    folderPath = "C:\path\to\files\"   'CHANGE THIS
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
    
    Set filesArray = CreateObject("System.Collections.ArrayList")
    
    fileName = Dir(folderPath & "*.xls")
    Do While fileName <> vbNullString
        fileNameDate = CDate(Left(fileName, 11))
        filesArray.Add CLng(fileNameDate) & "|" & fileName
        fileName = Dir()
    Loop
    
    filesArray.Sort
    filesArray.Reverse
    
    Debug.Print "Most recent: " & Split(filesArray(0), "|")(1)
    Debug.Print "4th most recent: " & Split(filesArray(3), "|")(1)
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,140
Messages
6,123,266
Members
449,093
Latest member
Vincent Khandagale

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