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 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"""