I'm processing data from a simulation. The monitors from the simulation are CSV files, but there are around 20 per simulation and it's cumbersome to upload each one in Excel and transform the values to decimal number type.
I currently have a VBA module that automates the upload of these values:
''' Sub Macro4()
ActiveWorkbook.Queries.Add Name:="oil-produced", Formula _
:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Table.FromColumns({Lines.FromBinary(File.Contents(""C:\Users\user\Documents\run6\results\oil-produced.out""), null, null, 1252)})," & Chr(13) & "" & Chr(10) & " #""Split Column by Delimiter"" = Table.SplitColumn(Source, ""Column1"", Splitter.SplitTextByDelimiter("" "", QuoteStyle.Csv), {""Column1.1"", ""C" & _
"olumn1.2"", ""Column1.3""})," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Split Column by Delimiter"",{{""Column1.1"", type number}, {""Column1.2"", type number}, {""Column1.3"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""oil-produced"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [oil-produced]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "oil_produced"
.Refresh BackgroundQuery:=False
End With
Application.Run "getUnits"
End Sub
The data file is in the folder C:\Users\user\Documents\run6\results\oil-produced and the data file is called oil-produced.
I have to call several data files like this and the folder that the files are in changes. I would like to be able to parameterize the file folder and data file name at the beginning of the macro so that I can change the file location easily without correcting each query and create a for loop to loop through the data files so that the sub isn't as long and bulky.
I tried doing this by making the folder name a string and substituting that within the workbook query; however, I get an error saying that the supplied file path must be a valid absolute path.
Does anyone have any suggestions for alternate ways to do this?