I'm having trouble finding a way to select the most recent version of several files in the same folder and importing them into Excel. For example:
Files in the folder: Spanish.csv Spanish(1).csv Spanish(2).csv English.csv English(1).csv French.csv (There are many more languages and files here, but I'm including only these for simplicity)
From that folder, I want to select these files: Spanish(2).csv English(1).csv French.csv
and import them into one existing worksheet.
So far I have:
Sub GetFiles()
Dim MyPath As String
Dim Spanish As String
Dim English As String
Dim French As String
Dim LanguageFiles(2) As String
MyPath = "C:\example\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
Spanish = Dir(MyPath & "Spanish*.csv")
English = Dir(MyPath & "English*.csv")
French = Dir(MyPath & "French*.csv")
I WANT TO SOMEHOW GET THE MOST RECENT VERSION OF EACH AND PASS IT TO THE LANGUAGEFILES ARRAY AND IMPORT IT TO A SINGLE WORKSHEET.
LanguageFiles(0) = Spanish
LanguageFiles(1) = English
LanguageFiles(2) = French
For i = LBound(LanguageFiles) To UBound(LanguageFiles)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & LanguageFiles(i), Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1, 0))
.Name = "Sample"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next i
End Sub
This code doesn't actually work because I'm cobbling several pieces together, and I don't know if I'm even on the right track. Can someone help me out please?