I keep getting the error message "subscript out of range", - excel

when I compare a table and a dump text file. I would load a powerbi to get a text file and then compareit with my already created table, so as to displaye the matching onesThis is the code that I ve been using to load the dump and compare it with the already created table
ActiveWorkbook.Queries.Add Name:="dump", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""xx:\location.txt""),[Delimiter=""#(tab)"", Columns=17, Encoding=65001, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""market"", Int64.Type}, {""category"", Int64.Type}, {""brand"", type text},{""brand status"", type text}, {""vendor_code"", type text}, {""status_last_update_date"", Int64.Type}})," & Chr(13) & "" & Chr(10) & " #""Filtered Rows"" = Table.SelectRows(#""Changed Type"", each ([brand status] <> """" and [brand_status] <> ""Audit"" and [brand status] <> ""SUPPRESSED""))," & Chr(13) & "" & Chr(10) & " " & _
Workbooks("Sheet2").Connections.Add2 "Query - dump", _
"Connection to the 'dump' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""us dump"";Extended Properties=""""" _
, "SELECT * FROM [us dump]", 2
ActiveWorkbook.Queries.Add Name:="Table1", Formula:= _

Related

How to update Source ID in ActiveWorkbook.Queries.Add

I'm trying to add parameters into a ActiveWorkbook.Queries.Add
I have managed to change all except the on in 'Source{[Id=""Table001""]}', inside the ActiveWorkbook.Queries.Add
I have tried Source{[Id=" & TableOnly & "]} but I get an error in line 'ActiveWorkbook.Queries.Add': The import table Table001 matches no exports
See below the full code and where 'Source{[Id=""Table001""]}' is located
For i = 1 to 3
TableName = "Table00" & i & " (Page " & i + 2 & ")" 'Example: "Table001 (Page 3)"
DispName = "Table00" & i & "__Page_" & i + 2 & "" 'Example: "Table001__Page_3"
TableOnly = "Table00" & i 'Example: Table001
ActiveWorkbook.Queries.Add Name:=TableName, Formula:= _
"let" & Chr(13) & "" & Chr(10) & _
" Source = Pdf.Tables(File.Contents(""C:\Sample.pdf""), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & _
" " & TableOnly & " = Source{[Id=""Table001""]}[Data]," & Chr(13) & "" & Chr(10) & _
" #""Changed Type"" = Table.TransformColumnTypes(" & TableOnly & ",{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", " & _
"type text}, {""Column6"", type text}})" & 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=" & TableName & ";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [" & TableName & "]")
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SaveData = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = DispName
.Refresh BackgroundQuery:=False
End With
Next
I found the problem. I had to add an addition inverted comas at each end as:
Source{[Id=""" & TableOnly & """]}
Hope is helps someone.

VBA QueryTables not working with Yahoo Finance

I'm trying this simple Sub (in Excel 2019) to import tables from a specific Yahoo Finance page.
It works with other websites, but with Yahoo Finance, I keep getting the same message "This Web query returned no data. To change..........." and no data is extracted.
I couldn't figure out the reason.
Sub ImportTable()
Dim ws As Worksheet
Dim qt As QueryTable
Dim qurl As String
qurl = "https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT"
Set ws = worksheets.Add
Set qt = ws.QueryTables.Add( _
Connection:="URL;" & qurl, _
Destination:=Range("A1"))
With qt
.RefreshOnFileOpen = False
.Name = "test"
.WebFormatting = xlWebFormattingNone
.WebSelectionType = xlAllTables
.Refresh
End With
End Sub
You can instead set-up connections to the various tables which will add them to the data model (Alt + B + M). In the queries pane of data you can then chose where to load them to your workbook if you wish to see them. You can also just work with them in the data model and generate your final desired output.
Note:
The quicker way is, from ribbon, to do, data > from web > enter url > select tables of interest. This will set-up table connections without vba.
VBA:
Option Explicit
Public Sub AddYahooFinanceTableConnections()
Dim wb As ThisWorkbook
Set wb = ThisWorkbook
wb.Queries.Add Name:="Balance Sheet", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data10 = Source{10}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data10,{{""Total Cash (mrq)"", type text}, {""131.99B"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="Cash Flow Statement", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data11 = Source{11}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data11,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="Dividends & Splits", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data5 = Source{5}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data5,{{""Forward Annual Dividend Rate"", type text}, {""2.24"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="Fiscal Year", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data6 = Source{6}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data6,{{""Column1"", type text}, {""Column2"", type date}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="Income Statement", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data9 = Source{9}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data9,{{""Revenue (ttm)"", type text}, {""153.28B"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="People Also Watch", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data12 = Source{12}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data12,{{""Symbol"", type text}, {""Last Price"", type number}, {""Change"", type number}, {""% Change"", Percentage.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="Profitability", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data7 = Source{7}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data7,{{""Column1"", type text}, {""Column2"", Percentage.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="Share Statistics", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data4 = Source{4}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data4,{{""Avg Vol (3 month)"", type text}, {""30.36M"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="Stock Price History", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data3 = Source{3}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data3,{{""Beta (5Y Monthly)"", type text}, {""0.79"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Queries.Add Name:="Table 1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & " Data1 = Source{1}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data1,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
wb.Connections.Add2 "Query - Balance Sheet", _
"Connection to the 'Balance Sheet' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Balance Sheet;Extended Properties=" _
, """Balance Sheet""", 6, True, False
wb.Connections.Add2 "Query - Cash Flow Statement", _
"Connection to the 'Cash Flow Statement' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Cash Flow Statement;Extended Properties=" _
, """Cash Flow Statement""", 6, True, False
wb.Connections.Add2 "Query - Dividends & Splits", _
"Connection to the 'Dividends & Splits' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Dividends & Splits;Extended Properties=" _
, """Dividends & Splits""", 6, True, False
wb.Connections.Add2 "Query - Fiscal Year", _
"Connection to the 'Fiscal Year' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Fiscal Year;Extended Properties=" _
, """Fiscal Year""", 6, True, False
wb.Connections.Add2 "Query - Income Statement", _
"Connection to the 'Income Statement' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Income Statement;Extended Properties=" _
, """Income Statement""", 6, True, False
wb.Connections.Add2 "Query - People Also Watch", _
"Connection to the 'People Also Watch' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=People Also Watch;Extended Properties=" _
, """People Also Watch""", 6, True, False
wb.Connections.Add2 "Query - Profitability", _
"Connection to the 'Profitability' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Profitability;Extended Properties=" _
, """Profitability""", 6, True, False
wb.Connections.Add2 "Query - Share Statistics", _
"Connection to the 'Share Statistics' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Share Statistics;Extended Properties=" _
, """Share Statistics""", 6, True, False
wb.Connections.Add2 "Query - Stock Price History", _
"Connection to the 'Stock Price History' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Stock Price History;Extended Properties=" _
, """Stock Price History""", 6, True, False
wb.Connections.Add2 "Query - Table 0", _
"Connection to the 'Table 0' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table 0;Extended Properties=" _
, """Table 0""", 6, True, False
wb.Connections.Add2 "Query - Table 1", _
"Connection to the 'Table 1' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table 1;Extended Properties=" _
, """Table 1""", 6, True, False
End Sub

How to fix Expression.Error: a cyclic reference was encountered during evaluation

In Excel VBA I've made an input box to create a new sheet, name it and create a table on said sheet. When I try to add the table to the data model it returns this error after creating the query.
ActiveWorkbook.Queries.Add Name:=strName, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.CurrentWorkbook(){[Name=" & strName & "]}[Content]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Date"", type any}, {""Time"", type any}, {""Call"", type any}, {""Greet"", type any}, {""Listen"", type any}, {""Engage"", type any}, {""Act"", type any}, {""Deliver"", type any}, {""Invite Back"", type any}, {""Total Score"", Int64.T" & _
"ype}, {""Notes"", type any}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
CurrentWorkbook.Connections.Add2 strName, _
"Connection to the " & strName & "query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & strName & ";Extended Properties="_
, "" & strName & "", 6, True, False
Where strName = InputBox("Enter Name")
Can anyone help me find/fix the cyclic reference?

How to fix "The value isn't a single character string" in power query

I'm trying to make my power query macro to work with looping data so I need to replace filepaths and references with variables.
Cell 37,1 has the location of the excel so the macro can run on different computers with simple path edit. The text file is an export from SAP.
I think the problems are in the last references.
Location=EH4
.CommandText = Array("SELECT * FROM [EH4]")
Sub GetDataEH4_2()
Dim FromFolder As String
FromFolder = Cells(37, 1).Value
Dim WorkCenter As String
WorkCenter = "EH4.txt"
Dim WC As String
WC = "EH4"
Dim FilePath As String
FilePath = FromFolder & WorkCenter
ActiveWorkbook.Queries(WC).Delete
ActiveWorkbook.Queries.Add Name:=WC, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""" & FilePath & """),
... Power query stuff, moving columns, removing rows, replacing cell values with "", adding custom row and determing column types ...
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:="OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=EH4;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [EH4]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = WC
.Refresh BackgroundQuery:=False
End With
With this code the error is "Run-time error '1004': [Expression.Error] The value isn't a single-character string."
I've tried changing .CommandText = Array("SELECT * FROM [EH4]") to
"SELECT * FROM" & WC ) but that gives "An operation that uses the database driver could not be completed"
Powerquery stuff here.
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""" & FilePath & """),[Delimiter="" "", Columns=22, Encoding=65001, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Col" & _
"umn6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text}, {""Column14"", type text}, {""Column15"", type text}, {""Column16"", type text}, {""Column17"", type text}, {""Column18"", type text}, {""Column19"", type text}, " & _
"{""Column20"", type text}, {""Column21"", type text}, {""Column22"", type text}})," & Chr(13) & "" & Chr(10) & " #""Removed Top Rows"" = Table.Skip(#""Changed Type"",7)," & Chr(13) & "" & Chr(10) & " #""Removed Columns"" = Table.RemoveColumns(#""Removed Top Rows"",{""Column1"", ""Column3"", ""Column6"", ""Column7"", ""Column10"", ""Column15"", ""Column17"", ""Column18"", ""Column21"", ""Column4""})," & Chr(13) & "" & Chr(10) & " #""Reorder" & _
"ed Columns"" = Table.ReorderColumns(#""Removed Columns"",{""Column20"", ""Column19"", ""Column2"", ""Column5"", ""Column8"", ""Column9"", ""Column11"", ""Column12"", ""Column13"", ""Column14"", ""Column16"", ""Column22""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value"" = Table.ReplaceValue(#""Reordered Columns"",""Total"","""",Replacer.ReplaceText,{""Column2""})," & Chr(13) & "" & Chr(10) & " #""Removed Blank R" & _
"ows"" = Table.SelectRows(#""Replaced Value"", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"""", null})))," & Chr(13) & "" & Chr(10) & " #""Replaced Value1"" = Table.ReplaceValue(#""Removed Blank Rows"",""User op. status"","""",Replacer.ReplaceText,{""Column20""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value2"" = Table.ReplaceValue(#""Replaced Value1"",""Stat"","""",Replacer.ReplaceTex" & _
"t,{""Column19""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value3"" = Table.ReplaceValue(#""Replaced Value2"",""Week"","""",Replacer.ReplaceText,{""Column2""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value4"" = Table.ReplaceValue(#""Replaced Value3"",""Work Ctr"","""",Replacer.ReplaceText,{""Column5""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value5"" = Table.ReplaceValue(#""Replaced Value4"",""Short text for order"","""",Replacer." & _
"ReplaceText,{""Column5""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value6"" = Table.ReplaceValue(#""Replaced Value5"",""Sales ord."","""",Replacer.ReplaceText,{""Column8""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value7"" = Table.ReplaceValue(#""Replaced Value6"",""Op."","""",Replacer.ReplaceText,{""Column9""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value8"" = Table.ReplaceValue(#""Replaced Value7"",""Operation text"","""",Repla" & _
"cer.ReplaceText,{""Column11""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value9"" = Table.ReplaceValue(#""Replaced Value8"",""UserFl"","""",Replacer.ReplaceText,{""Column12""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value10"" = Table.ReplaceValue(#""Replaced Value9"",""User field (20) 1"","""",Replacer.ReplaceText,{""Column13""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value11"" = Table.ReplaceValue(#""Replaced Value10"","" Work""" & _
","""",Replacer.ReplaceText,{""Column14""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value12"" = Table.ReplaceValue(#""Replaced Value11"",""LatestFin."","""",Replacer.ReplaceText,{""Column16""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value13"" = Table.ReplaceValue(#""Replaced Value12"",""Usr date 2"","""",Replacer.ReplaceText,{""Column22""})," & Chr(13) & "" & Chr(10) & " #""Renamed Columns"" = Table.RenameColumns(#""Replaced Value13" & _
""",{{""Column20"", ""System status""}, {""Column19"", ""User status""}, {""Column2"", ""Week""}, {""Column5"", ""Description""}, {""Column8"", ""Sales document""}, {""Column9"", ""Activity""}, {""Column11"", ""Description activity""}, {""Column12"", ""Eng category""}, {""Column13"", ""Engineer name""}, {""Column14"", ""Work hours""}, {""Column16"", ""Latest finish d" & _
"ate""}, {""Column22"", ""Est. finished""}})," & Chr(13) & "" & Chr(10) & " #""Added Custom"" = Table.AddColumn(#""Renamed Columns"", ""Comments"", each """")," & Chr(13) & "" & Chr(10) & " #""Changed Type1"" = Table.TransformColumnTypes(#""Added Custom"",{{""Latest finish date"", type date}, {""Est. finished"", type date}, {""Comments"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type1"""
The same error can come out when trying to record a MACRO to import data to power query.
In my situation, I figured out that the Delimiter="" "" changed to Delimiter=""#(tab)"" solved my problem.

Record Macro: Get Data from File

I'll start by admitting Macro's are generally not my thing and writing the code certainly isn't - but in the situation and having read about recording macros i thought it could work for me. If this isn't the way to do it feel free to say so and i will dismiss the idea for the time being until i can do more research and write the code.
Essentially the steps I'm trying to achieve
- Starting from a blank workbook
- Use macro to get data from a folder in our company network containing about 15 workbooks which all have the same internal format and information but relate to different geographic locations
- I recorded the macro using exactly the steps i use if i were doing the task manually which i know works fine
the full code text is below. Upon running i immediately get a run-time error which when debugged highlights the first section and states "A query with the name 'Pipeline Working' already exists. "Pipeline Working" is the name of the folder in which contains the workbooks to import, from there I'm already lost.
Any help appreciated.
Sub PipelinePredictions()
'
' PipelinePredictions Macro
' Import Data from File for Pipeline Predictions
'
'
ActiveWorkbook.Queries.Add Name:="Pipeline Working", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""\\hofileserver\Accounts\ACCOUNTS\Lettings Stats 2018 Qtr 4\Pipelines\Pipeline Working"")," & Chr(13) & "" & Chr(10) & " #""Filtered Hidden Files1"" = Table.SelectRows(Source, each [Attributes]?[Hidden]? <> true)," & Chr(13) & "" & Chr(10) & " #""Invoke Custom Function1"" = Table.AddColumn(#""Filtered Hidden Files1"", ""Transform File from Pipeline Working"", each #""Transform File f" & _
"rom Pipeline Working""([Content]))," & Chr(13) & "" & Chr(10) & " #""Renamed Columns1"" = Table.RenameColumns(#""Invoke Custom Function1"", {""Name"", ""Source.Name""})," & Chr(13) & "" & Chr(10) & " #""Removed Other Columns1"" = Table.SelectColumns(#""Renamed Columns1"", {""Source.Name"", ""Transform File from Pipeline Working""})," & Chr(13) & "" & Chr(10) & " #""Expanded Table Column1"" = Table.ExpandTableColumn(#""Removed Other Columns" & _
"1"", ""Transform File from Pipeline Working"", Table.ColumnNames(#""Transform File from Pipeline Working""(#""Sample File"")))," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Expanded Table Column1"",{{""Source.Name"", type text}, {""Column1"", type any}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type number}" & _
", {""Column6"", Percentage.Type}, {""Column7"", Int64.Type}, {""Column8"", type number}, {""Column9"", type number}, {""Column10"", type number}, {""Column11"", type any}, {""Column12"", type datetime}, {""Column13"", type text}})," & Chr(13) & "" & Chr(10) & " #""Renamed Columns"" = Table.RenameColumns(#""Changed Type"",{{""Source.Name"", ""Branch""}, {""Column1"", ""Week Booked""}, {""Col" & _
"umn2"", ""Neg""}, {""Column3"", ""Property""}, {""Column4"", ""Service Type""}, {""Column5"", ""Rent""}, {""Column6"", ""Percentage Fee""}, {""Column7"", ""Term""}, {""Column8"", ""Fee""}, {""Column9"", ""Admin""}, {""Column10"", ""Total Fees""}, {""Column11"", ""Rent G'tee""}})," & Chr(13) & "" & Chr(10) & " #""Changed Type1"" = Table.TransformColumnTypes(#""Renamed Columns"",{{""Column12" & _
""", type date}})," & Chr(13) & "" & Chr(10) & " #""Renamed Columns2"" = Table.RenameColumns(#""Changed Type1"",{{""Column12"", ""Move in Date""}, {""Column13"", ""Notes""}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Renamed Columns2"""
ActiveWorkbook.Queries.Add Name:="Sample File Parameter1", Formula:= _
"#""Sample File"" meta [IsParameterQuery=true, BinaryIdentifier=#""Sample File"", Type=""Binary"", IsParameterQueryRequired=true]"
ActiveWorkbook.Queries.Add Name:= _
"Transform Sample File from Pipeline Working", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(#""Sample File Parameter1"", null, true)," & Chr(13) & "" & Chr(10) & " #""'A Pipeline$'Print_Area1"" = Source{[Name=""'A Pipeline$'Print_Area""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""'A Pipeline$'Print_Area1"""
ActiveWorkbook.Queries.Add Name:="Sample File", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""\\hofileserver\Accounts\ACCOUNTS\Lettings Stats 2018 Qtr 4\Pipelines\Pipeline Working"")," & Chr(13) & "" & Chr(10) & " Navigation1 = Source{0}[Content]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Navigation1"
ActiveWorkbook.Queries.Add Name:="Transform File from Pipeline Working", _
Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = (#""Sample File Parameter1"") => let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(#""Sample File Parameter1"", null, true)," & Chr(13) & "" & Chr(10) & " #""'A Pipeline$'Print_Area1"" = Source{[Name=""'A Pipeline$'Print_Area""]}[Data]" & Chr(13) & "" & Chr(10) & " in" & Chr(13) & "" & Chr(10) & " #""'A Pipeline$'Print_Area1""" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Pipeline Working"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Pipeline Working]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_ExternalData_1"
.Refresh BackgroundQuery:=False
End With
Workbooks("Book1").Connections.Add2 "Query - Sample File Parameter11", _
"Connection to the 'Sample File Parameter1' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File Parameter1"";Extended Properties=""""" _
, "SELECT * FROM [Sample File Parameter1]", 2
Workbooks("Book1").Connections.Add2 _
"Query - Transform Sample File from Pipeline Working1", _
"Connection to the 'Transform Sample File from Pipeline Working' query in the workbook." _
, Array( _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform Sample File from Pipeline Working"";Extended Propert" _
, "ies="""""), "SELECT * FROM [Transform Sample File from Pipeline Working]", 2
Workbooks("Book1").Connections.Add2 "Query - Sample File1", _
"Connection to the 'Sample File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File"";Extended Properties=""""" _
, "SELECT * FROM [Sample File]", 2
Workbooks("Book1").Connections.Add2 _
"Query - Transform File from Pipeline Working1", _
"Connection to the 'Transform File from Pipeline Working' query in the workbook." _
, _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform File from Pipeline Working"";Extended Properties=""""" _
, "SELECT * FROM [Transform File from Pipeline Working]", 2
Windows("Book3").Activate
Range("A1").Select
End Sub
This opens all workbooks in folder and copies the range called print area into the current spreadsheet - it assumes this code is in a blank workbook
Sub ImportData()
Const FolderToImport = "C:\Pipeline Working\" 'adjust as suits - keep last \ at end
Dim s As String
s = Dir(FolderToImport & "*.xl??")
Do While s <> ""
Dim wb As Workbook
Set wb = Workbooks.Open(FolderToImport & s, False, True)
wb.Sheets(1).Range("print area").Copy Destination:= _
ThisWorkbook.Worksheets(1).Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
wb.Close False
s = Dir()
Loop
End Sub

Resources