I have been test the following macro. It originates from a recorded macro by running QueryTable, and is intended to get data table for an appointed stock from a site.
Some information about the site for one stock is as follows.
stock code & structure of the tables
I modified the original macro, making the stock code, the table number and table date into arguments, so that a loop could be invoked to get tables with the assigned "table number/table date" for a whole list of stock codes.
Following is the modified macro.
Sub Fetch_Table(code As String, tableDate As String)
Dim sourceFullName As String
sourceFullName = "http://emweb.securities.eastmoney.com/PC_HSF10/BusinessAnalysis/Index?type=web&code=" & code
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""" & sourceFullName & """))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""" & tableDate & """, type text},{""主营构成"", type text}, {""主营收入(元)"", type text}, {""收入比例"", Percentage.Type}, {""主营成本(元)"", type text}, {""成本比" & _
"例"", type text}, {""主营利润(元)"", type text}, {""利润比例"", type text}, {""毛利率(%)"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0"";Extended Properties=""""" _
, Destination:=Range("$A$5")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 0]")
.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_0"
.Refresh BackgroundQuery:=False
End With
End Sub
The code As String, tableDate as String work well. And I call Fetch_Table(code,tableDate), with this two parameters passed.
Then I tried to turn the number in Source{0}[Data] in to a reference, which means I can choose which talbe to fetch, and make the number match the date of the table. So I tested the following code, which treated N as a local variable temporarily, before making it into a parameter. All the rest are same as the above well working macro.
Sub Fetch_Table(code As String, tableDate As String)
Dim sourceFullName As String
sourceFullName = "http://emweb.securities.eastmoney.com/PC_HSF10/BusinessAnalysis/Index?type=web&code=" & code
N = 0
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""" & sourceFullName & """))," & Chr(13) & "" & Chr(10) & " Data0 = Source{N}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""" & tableDate & """, type text},{""主营构成"", type text}, {""主营收入(元)"", type text}, {""收入比例"", Percentage.Type}, {""主营成本(元)"", type text}, {""成本比" & _
"例"", type text}, {""主营利润(元)"", type text}, {""利润比例"", type text}, {""毛利率(%)"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0"";Extended Properties=""""" _
, Destination:=Range("$A$5")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 0]")
.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_0"
.Refresh BackgroundQuery:=False
End With
End Sub
However, when I called this macro again, an error msgbox came out.
enter image description here
At first, I tried to explicited define the type of N as integer, and then I tried to change the type of N into string. But they all failed.
I assumed that the nubmer in Source{0}[Data] could be taken palce by a variable, and the prolem stemmed from syntax mistakes.
I don't know whether my assumption is right.
I want to run a macros to get data from a PDF file, and after extract data from PDF file, I also need a code to get data from queries like this, See screenshot below.
queuries
***Everytime, the data from PDF has different tables and pages, so how can I run the macros for different pdf file with different number of tables and pages
Sub Macro1()
'
' Macro1 Macro
'
'
ActiveWorkbook.Queries.Add Name:="Table001 (Page 1)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table001 = Source{[Id=""Table001""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table001,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table002 (Page 1)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table002 = Source{[Id=""Table002""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table002,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table003 (Page 1)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table003 = Source{[Id=""Table003""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table003,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type duration}, {""Column6""," & _
" type text}, {""Column7"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table004 (Page 1)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table004 = Source{[Id=""Table004""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table004,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table005 (Page 2)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table005 = Source{[Id=""Table005""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table005,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table006 (Page 3)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table006 = Source{[Id=""Table006""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table006,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table007 (Page 4)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table007 = Source{[Id=""Table007""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table007,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table008 (Page 4)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table008 = Source{[Id=""Table008""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table008,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table009 (Page 4)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table009 = Source{[Id=""Table009""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table009,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type duration}, {""Column6""," & _
" type text}, {""Column7"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table010 (Page 4)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table010 = Source{[Id=""Table010""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table010,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table011 (Page 5)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table011 = Source{[Id=""Table011""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table011,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table012 (Page 5)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table012 = Source{[Id=""Table012""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table012,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table013 (Page 5)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table013 = Source{[Id=""Table013""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table013,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type duration}, {""Column6""," & _
" type text}, {""Column7"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table014 (Page 5)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table014 = Source{[Id=""Table014""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table014,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table015 (Page 6)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table015 = Source{[Id=""Table015""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table015,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table016 (Page 7)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table016 = Source{[Id=""Table016""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table016,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table017 (Page 8)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table017 = Source{[Id=""Table017""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table017,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table001 (Page 1)", _
"Connection to the 'Table001 (Page 1)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table001 (Page 1);Extended Properties=" _
, """Table001 (Page 1)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table002 (Page 1)", _
"Connection to the 'Table002 (Page 1)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table002 (Page 1);Extended Properties=" _
, """Table002 (Page 1)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table003 (Page 1)", _
"Connection to the 'Table003 (Page 1)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table003 (Page 1);Extended Properties=" _
, """Table003 (Page 1)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table004 (Page 1)", _
"Connection to the 'Table004 (Page 1)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table004 (Page 1);Extended Properties=" _
, """Table004 (Page 1)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table005 (Page 2)", _
"Connection to the 'Table005 (Page 2)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table005 (Page 2);Extended Properties=" _
, """Table005 (Page 2)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table006 (Page 3)", _
"Connection to the 'Table006 (Page 3)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table006 (Page 3);Extended Properties=" _
, """Table006 (Page 3)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table007 (Page 4)", _
"Connection to the 'Table007 (Page 4)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table007 (Page 4);Extended Properties=" _
, """Table007 (Page 4)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table008 (Page 4)", _
"Connection to the 'Table008 (Page 4)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table008 (Page 4);Extended Properties=" _
, """Table008 (Page 4)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table009 (Page 4)", _
"Connection to the 'Table009 (Page 4)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table009 (Page 4);Extended Properties=" _
, """Table009 (Page 4)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table010 (Page 4)", _
"Connection to the 'Table010 (Page 4)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table010 (Page 4);Extended Properties=" _
, """Table010 (Page 4)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table011 (Page 5)", _
"Connection to the 'Table011 (Page 5)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table011 (Page 5);Extended Properties=" _
, """Table011 (Page 5)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table012 (Page 5)", _
"Connection to the 'Table012 (Page 5)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table012 (Page 5);Extended Properties=" _
, """Table012 (Page 5)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table013 (Page 5)", _
"Connection to the 'Table013 (Page 5)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table013 (Page 5);Extended Properties=" _
, """Table013 (Page 5)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table014 (Page 5)", _
"Connection to the 'Table014 (Page 5)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table014 (Page 5);Extended Properties=" _
, """Table014 (Page 5)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table015 (Page 6)", _
"Connection to the 'Table015 (Page 6)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table015 (Page 6);Extended Properties=" _
, """Table015 (Page 6)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table016 (Page 7)", _
"Connection to the 'Table016 (Page 7)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table016 (Page 7);Extended Properties=" _
, """Table016 (Page 7)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table017 (Page 8)", _
"Connection to the 'Table017 (Page 8)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table017 (Page 8);Extended Properties=" _
, """Table017 (Page 8)""", 6, True, False
ActiveWorkbook.Queries.Add Name:="Append1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Table.Combine({#""Table001 (Page 1)"", #""Table002 (Page 1)"", #""Table003 (Page 1)"", #""Table004 (Page 1)"", #""Table005 (Page 2)"", #""Table006 (Page 3)"", #""Table007 (Page 4)"", #""Table008 (Page 4)"", #""Table009 (Page 4)"", #""Table010 (Page 4)"", #""Table011 (Page 5)"", #""Table012 (Page 5)"", #""Table013 (Page 5)"", #""Table014 (Page 5)""," & _
" #""Table015 (Page 6)"", #""Table016 (Page 7)"", #""Table017 (Page 8)""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source" & _
""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Append1;Extended Properties=""""" _
, Destination:=Range("$A$2")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Append1]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Append1"
.Refresh BackgroundQuery:=False
End With
End Sub
I have a Power Query in which I'm trying to get the link in a cell Z13 from "Shift Sched" sheet. The link in Z13 will get me the excel file in which I have the data. Cell name is defined as REFCELL
Here is a wat i got from record macro.
Sub powerquery()
'
' powerquery Macro
'
'
Range("W13").Select
ActiveWorkbook.Queries.Add Name:="Schedules", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(Web.Contents(""https://portal.ant.website.com/sites/wfl/Shift Schedules/Shift Schedules and Emp Details w.e.f Jun 03, 2018-HYD.xlsx""), null, true)," & Chr(13) & "" & Chr(10) & " Schedules_Sheet = Source{[Item=""Schedules"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Schedules_Sheet,{{""Column1"", type text}, {""Column2"", type" & _
" text}, {""Column3"", type text}, {""Column4"", type any}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type any}, {""Column8"", type any}, {""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 any}, {""Column20"", type text}, {""Column21"", type text}, {""Column22"", type text}, {""Column23"", type any}, {""Column24"", type any}, {""Column25"", type any}, {""Column26"", type any}, {""Column27"", type any}, {""Column28"", type any}, {""Column29"", type any}, {""Column30"", ty" & _
"pe any}, {""Column31"", type text}, {""Column32"", type any}, {""Column33"", type text}, {""Column34"", type text}, {""Column35"", type text}, {""Column36"", type any}, {""Column37"", type text}, {""Column38"", type text}, {""Column39"", type any}, {""Column40"", type any}, {""Column41"", type any}, {""Column42"", type text}, {""Column43"", type any}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""C" & _
"hanged Type"""
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Schedules;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Schedules]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Schedules_2"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End Sub
**This is not working for me because the link keeps changing depending on the date ex. Jun 03, 2018. The link has to be taken from the cell z13 in "Shift Sched" Sheet and load in a new sheet.
It's nearly all related to getting your "" quotation marks in the right place to asemble your query string correctly. Try this:
ActiveWorkbook.Queries.Add _
Name:="Schedules", _
Formula:="let" & vbCr & _
vbTab & "MyURL = Excel.CurrentWorkbook(){[Name=""REFCELL""]}[Content][Column1]{0}," & vbCr & _
vbTab & "Source = Web.Page(Web.Contents(MyURL))" & vbCr & _
"in Source"