i would to import the last modified txt file from a directory using a macro in Excel.
I have a folder which is incremented everyday by a new txt file.
The goal is to import the last txt file added in the direrctory.
I’ve already created a an Excel file with a button affected to macro.
Here is the code of macro:
Sub Import()
'
' Import Macro
' Macro saved on 02/03/2011 by StYellowknife3000
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Folder\File_01.txt", Destination:= _
Range("A1"))
.Name = "File_01"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 932
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Thank you
One way to do it is to use the Scripting.FileSystemObject and loop through all the files check their dates. Here’s some code I use to open the latest CSV in a folder
You can see all the code and the references you need to set here
http://www.dailydoseofexcel.com/archives/2009/05/01/opening-the-newest-file-in-a-folder-with-vba/