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
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.
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.
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