Wondering if there is a way to do this. I am looking to add multiple csv files to a single files data model. I have tried both the Listobjects method and query ables methods each seem to come just short of what I need. Listobjects successfully adds the connection to the model but to add the connection via this method I must go through the Text Import Wizard Setup with AddFromFile connection as it needs the delimit instructions. As the file is pulling from multiple files and it needs to be used on multiple desktops, setting up connections with the wizard each time makes this method unfeasible. Anyway to code Text Import Wizard portion? The function properties for the command do not seem to allow for it. The other method, query tables, successfully adds the data sets to my current file but not the data to the singular files data model. Could one take that and add it to the data model after the fact? I am not seeing a way to code that either?
Both examples below are static examples pulling just one file. I aimed to keep the examples simple for my particular problem rather than show the variables and For loop I would set up to cascade the import for multiple files.
List Objects code example:
ThisWorkbook.Connections.AddFromFile _
"C:\Computer\User\Project\Data\Test1.csv", True, False
With ActiveSheet.ListObjects.Add(SourceType:=4, Source:=ActiveWorkbook. _
Connections("Test1"), Destination:=Range("$A$1")).TableObject
.RowNumbers = False
.PreserveFormatting = True
.RefreshStyle = 1
.AdjustColumnWidth = True
.ListObject.DisplayName = "Test1"
.Refresh
End With
Querytables code example:
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & "C:\Computer\User\Project\Data\Test1.csv", Destination:=Range("$A$1"))
.Name = "Test1"
.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 = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
The big question is there a way to do what I am seeking within either route or is there another way to accomplish this that I am unaware of? Thanks