Avatar of Emily Barnes
Emily Barnes
 asked on

How to select files in a folder (to get data from), selecting the most updated version based on name of file

I have a code to extract certain data from files in a folder but I would like to select the updated versions of these files (there are ten in total, with newer versions added frequently). The newer versions have higher numbers in the name so for example, the name of one file could be T1_D002 and then T1_D003 would be the new version of the TP1 files in the folder and this would be the case all the way to TP10. I already have a loop going through the folder to select certain files, but it will give all of the versions of the T1-T10's. The T and D parts are not the only characters in the file name. Any help would be wonderful.
VBAMicrosoft Excel

Avatar of undefined
Last Comment
Emily Barnes

8/22/2022 - Mon
Ryan Chong

The T and D parts are not the only characters in the file name
so can you provide us few real samples and its formats?
Fabrice Lambert

Beside filtering, are file names format enough as criteria ?
What about files creation date and / or files last modified date ?
Emily Barnes

ASKER
[sample][/filename]

So an example would be: ABCDEF_123m_TP1_D003_AOB

The ABCDEF won't always necessarily be that number of letters and the same for the numbers and the AOB can be different too but I have already got it sorted to only pull those files with a certain last few characters.

The date last modified won't always necessarily be the latest version in the folder so I can't use a date finding method. The names themselves should be a clear filter.

Thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Bill Prew

It would be good if you shared the existing code that finds the files too, might make it easier for the experts to propose changes or additions that would fit better into that code to find the most recent files.


»bp
ASKER CERTIFIED SOLUTION
Fabrice Lambert

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Emily Barnes

ASKER
Wow thank you! I have a very limited knowledge of VBA and macros, so this was the code I was working from, I was going to repeat to TP10.
How would your code Fabrice fit in with this, I wouldn't have to repeat/copy it for each T number?

Sub LoopThroughFolder()
   
    Dim Location As String
    Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
    Dim Rws As Long, Rng As Range
    Dim MyPath As String
    Dim LatestFile As String
    Dim LatestDate As Date
    Dim LMD As Date
    Set Wb = ThisWorkbook
   
    Location = Sheets("Sheet1").Range("A1").Value & "\"
   
    'change the address to suite
    MyDir = Location
    MyFile = Dir(MyDir & "*TP1??????POMM.csv")       'change file extension
    ChDir MyDir
    Application.ScreenUpdating = 0
    Application.DisplayAlerts = 0

   
     Do While Len(MyFile) > 0
     LMD = FileDateTime(MyPath & MyFile)
     If LMD > LatestDate Then
            LatestFile = MyFile
            LatestDate = LMD
        End If
          MyFile = Dir
       
    Loop
        Do While MyFile <> ""
        Workbooks.Open (MyFile)
        With Worksheets(1)
            Rws = .Cells(Rows.Count, "C").End(xlUp).Row
            Set Rng = Range("C4:J4")
            Rng.Copy Wb.Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) ' "C" = column to start pasting
            ActiveWorkbook.Close True
        End With
        MyFile = Dir()
    Loop
   
    'Call Remove_Zeros
    'change the address to suite
    MyDir = Location
    MyFile = Dir(MyDir & "*TP2??????POMM.csv")       'change file extension
    ChDir MyDir
    Application.ScreenUpdating = 0
    Application.DisplayAlerts = 0

    Do While MyFile <> ""
        Workbooks.Open (MyFile)
        With Worksheets(1)
            Rws = .Cells(Rows.Count, "C").End(xlUp).Row
            Set Rng = Range("C4:J4")
            Rng.Copy Wb.Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) ' "C" = column to start pasting
            ActiveWorkbook.Close True
        End With
        MyFile = Dir()
    Loop
   
    'Call Remove_Zeros
'change the address to suite
    MyDir = Location
    MyFile = Dir(MyDir & "*TP3??????POMM.csv")       'change file extension
    ChDir MyDir
    Application.ScreenUpdating = 0
    Application.DisplayAlerts = 0

    Do While MyFile <> ""
        Workbooks.Open (MyFile)
        With Worksheets(1)
            Rws = .Cells(Rows.Count, "C").End(xlUp).Row
            Set Rng = Range("C4:J4")
            Rng.Copy Wb.Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) ' "C" = column to start pasting
            ActiveWorkbook.Close True
        End With
        MyFile = Dir()
    Loop
   
    'Call Remove_Zeros