alsdkjfladjfgj12321
New Member
- Joined
- Jun 20, 2019
- Messages
- 7
I wrote some VBA to that adds a .csv file as a power query in Excel. The file location can change. I have some code that allows you to pick where the files are located, but I can't figure how to use that input to tell it where to look.
Below is what I have for getting the file location.
Dim fd As FileDialog
Dim sPath As String
Dim GetFileName As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
If fd.Show = -1 Then
sPath = fd.SelectedItems(1)
End If
'sPath now holds the path to the folder or nothing if the user clicked the cancel button
'MsgBox sPath
This is what I have for importing the file.
ActiveWorkbook.Queries.Add Name:="FileName", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""C:\CAN\CHANGE\STAYS\SAME\FileName.csv""),[Delimiter="","", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Hea" & _
"ders"",{{""Part"", type text}, {""Color"", Int64.Type}, {""Quantity"", Int64.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Workbooks(GetFileName).Connections.Add2 "Query - FileName", _
"Connection to the 'FileName' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""FileName"";Extended Properties=""""" _
, "SELECT * FROM [FileName]", 2
I'm not sure that is good way to approach this. I hope I explained this well enough to understand.
Below is what I have for getting the file location.
Dim fd As FileDialog
Dim sPath As String
Dim GetFileName As String
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
If fd.Show = -1 Then
sPath = fd.SelectedItems(1)
End If
'sPath now holds the path to the folder or nothing if the user clicked the cancel button
'MsgBox sPath
This is what I have for importing the file.
ActiveWorkbook.Queries.Add Name:="FileName", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""C:\CAN\CHANGE\STAYS\SAME\FileName.csv""),[Delimiter="","", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Hea" & _
"ders"",{{""Part"", type text}, {""Color"", Int64.Type}, {""Quantity"", Int64.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Workbooks(GetFileName).Connections.Add2 "Query - FileName", _
"Connection to the 'FileName' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""FileName"";Extended Properties=""""" _
, "SELECT * FROM [FileName]", 2
I'm not sure that is good way to approach this. I hope I explained this well enough to understand.