I've got many CSV files and I want to use macro to import them (and manipulate them) into a existing excel file.
The thing is that I only know a part of the files. They are like :
- "ThisIsTheFirstFile-01234564893.csv"
- "ThisIsTheSecondFile-015648942314.csv" ...
So I used this code to import my file :
Sub LoadFromFile()
Dim fileName As String, folder As String
folder = "C:\FolderPath\"
fileName = "ThisIsTheFirstFile-*"
Dim file As String
file = Dir$(folder & fileName & ".*")
ActiveCell.Offset(0, 0).Range("A1").Select
With ActiveSheet.QueryTables _
.Add(Connection:="TEXT;" & file, Destination:=ActiveCell)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
But it doesn't seem to match the filename. For now I just wish to load one file, I'll then manipulate it, exctract data, supress all data loaded to load others files after.