Import text file from drop-down in Excel - vba

I am trying to create a macro in Excel that imports a text file based on values in cells on a worksheet. One cell specifies the path of the directory, a second cell with a drop-down contains the file's name including extension.
I've tried to record a macro wherein I import a file from the directory in question, but I am having trouble changing it so that it uses the two cells with the path and file name instead of constant values.
The original recorded macro looks as follows:
Sub txt_import()
' txt_import Macro
ActiveWorkbook.Queries.Add Name:="testfile", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""D:\testpath\testfile.txt""),[Delimiter="";"", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Change Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Colu" & _
"mn4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Change Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=testfile;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [testfile]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "_testfile"
.Refresh BackgroundQuery:=False
End With
End Sub
The directory path is located on Sheet1 A1 and the drop-down list with the file names is in Sheet3 A2.
Any help is greatly appreciated!

You'll need to modify the Source query string, try:
Sub txt_import()
' txt_import Macro
Dim path as String, filename as String, fullFileName as String
path = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
filename = ThisWorkbook.Worksheets("Sheet3").Range("A2").Value
fullFileName = path & Application.PathSeparator & filename
ActiveWorkbook.Queries.Add Name:="testfile", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(" & fullFileName & "),[Delimiter="";"", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Change Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Colu" & _
"mn4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Change Type"""

Related

How to turn the number inside Source{0}[Data] into a variable?

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.

VBA extract data from Power Queries of PDF file

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

Cell reference to a powerquery

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"

Error when refreshing query

I'm recording a macro to automate the process of gathering P/E ratio information from a website for various stock tickers. The macro involves two web queries. I recorded them both the exact same way, and they both use the same URL. One of these queries works fine, and the other gives an
error 1004 "Application-defined or object-defined error"
I have pasted the code for that query below.
ActiveWorkbook.Queries.Add Name:="MSFT PE Ratio (TTM) Range, Past 5 Years", _
Formula:= _
"let" & Chr(13) & "" & Chr(10) & "
Source = Web.Page(Web.Contents(""https://ycharts.com/companies/" & ActiveCell.Value & "/pe_ratio""))," & Chr(13) & "" & Chr(10) & " Data3 = Source{3}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data3,{{""Column1"", type text}, {""Column2"", type number}, {""Column3"", type date}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = "query2"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""" & ActiveCell.Value & "PE Ratio (TTM) Range, Past 5 Years"";Extended Properties=" _
, """"""), Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT * FROM [" & ActiveCell.Value & "PE Ratio (TTM) Range, Past 5 Years]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "_PE_Ratio__TTM__Range__Past_5_Years"
**.Refresh BackgroundQuery:=False** <-- this is where I get the error
End With
This is code for one web query and one database query. Not two webqueries.
Both querytypes can make use of parameters which you can attach to a cell and set to automatically refresh the querytable when that cell changes.
See: Webquery with parameters and http://dailydoseofexcel.com/archives/2004/12/13/parameters-in-excel-external-data-queries/

Get table from website using VBA with dynamic web address

I'm trying to insert a table from a website into excel, specifically off of WSJ futures. There's a calendar feature I can get around by changing the web address. I would like to make the website in the code a cell reference so I can make the address dynamic. Below is the code for the regular macro with no dynamic address. Suggestions on how to make this change? I've tried taking out the URL and making it a cell reference, but it did not work.
ActiveWorkbook.Queries.Add Name:=Range("A1"), Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""http://www.wsj.com/mdc/public/page/2_3023-fut_metal-futures-20170901.html?mod=mdc_pastcalendar""))," & Chr(13) & "" & Chr(10) & " Data5 = Source{5}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data5,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Colu" & _
"mn6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})," & Chr(13) & "" & Chr(10) & " #""Removed Top Rows"" = Table.Skip(#""Changed Type"",1)," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(#""Removed Top Rows"", [PromoteAllScalars=true])" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Promoted Headers"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 5"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 5]")
.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_5"
.Refresh BackgroundQuery:=False
End With
End Sub