I've got a mixture of VBA and a macro to retrieve a website table, but it doesn't update the table name when it runs and has to be manually changed before running. Any help would be greatly appreciated.
Sub NZXMarket()
'
' NZXMarket Macro
' Daily list of NZX market
'
'
ActiveWorkbook.Queries.Add Name:="Table 0 (4)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://www.nzx.com/markets/NZSX""))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Code"", type text}, {""Company"", type text}, {""Price"", Currency.Type}, {""Change"", type text}, {""Volume"", type number}, {""Value"", Currency.Type}, {""Capitalisation"", Currency.Type}, {""Pe" & _
"rcentage Change"", type number}, {""Type"", type text}, {""Green Bond"", type logical}, {""Trade Count"", Int64.Type}, {""Currency Code"", type text}, {""Market Capitalisation"", Currency.Type}})," & Chr(13) & "" & Chr(10) & " #""Removed Columns"" = Table.RemoveColumns(#""Changed Type"",{""Green Bond"", ""Currency Code""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Removed Columns"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0 (2)"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 0 (2)]")
.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__4"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.Name = Format(Date, "DD-MM-YY")
MsgBox "Download Complete"
End Sub
Related
My issue is that I want to be able to load as many pages as the file has. I'm stuck on this, and even though it says pdf.tables has the ability to default to all pages loading starting with one, whenever I try that it just has a connection error on the query. Each pdf this macro is for has varying pages, and the rest of my code cleans up the data, but I just can't get it to load in all pages which is all I want it to do, a simple load all function. I'm loosing my marbles, I haven't had issues like this with any other language, no offense to any VBA lovers out there. Thank you in advance for the help.
Here is my sample code:
ActiveWorkbook.Queries.Add name:="Page001", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""pdf.pdf""), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Page1 = Source{[Id=""Page001""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Page1, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promot" & _
"ed Headers"",{{""Column1"", type text}, {""Column2"", Int64.Type}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", Int64.Type}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text}, {""Clean S" & _
"hip"", type text}, {""Column15"", type text}, {""Column16"", type text}})," & Chr(13) & "" & Chr(10) & " #""Appended Query"" = Table.Combine({#""Changed Type"", Page002})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Appended Query"""
ActiveWorkbook.Queries.Add name:="Page002", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""pdf.pdf""), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Page1 = Source{[Id=""Page002""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Page1, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promot" & _
"ed Headers"",{{""Route #"", type text}, {""Column2"", type text}, {""Description"", type text}, {""Column4"", type text}, {""Size"", type text}, {""Unit"", type text}, {""Action"", type text}, {""Column8"", type text}, {""Total"", type text}, {""Column10"", Int64.Type}})" & 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=Page001;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Page001]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Page001"
.Refresh BackgroundQuery:=False
End With
ActiveWorkbook.Worksheets.Add
With activeSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Page002;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Page002]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Page002"
.Refresh BackgroundQuery:=False
End With
the following code sets cell B21 to a variable and then reference that variable to download data from the website.
The error I get:
Code with Variable (broken):
Error: Run-time error '1004':
[Expression.Error] The import TICKER matches no exports. Did you miss a module reference?
Sub DownloadDataV5()
'
' DownloadDataV5 Macro
Range("B21").Select
Selection.Copy
' WEBLINK CODE that takes data from cell that combines TICKER & START DATE & ENDING DATE
Dim WEBLINK As String
WEBLINK = Sheets("Download").Cells(21, "B").Value
' comment
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents((TICKER)))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Buy/Sell"", type text}, {""Transaction Date"", type date}, {""Acceptance DateTime"", type" & _
" datetime}, {""Issuer Name"", type text}, {""Issuer Trading Symbol"", type text}, {""Reporting Owner Name"", type text}, {""Reporting Owner Relationship"", type text}, {""Transaction Shares"", Int64.Type}, {""Price per Share"", Currency.Type}, {""Total Value"", Currency.Type}, {""Shares Owned Following Transaction"", Int64.Type}, {""Form"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""" & _
"Changed Type"""
ActiveWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "MySheet"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0"";Extended Properties=""""" _
, Destination:=Range("$A$1")).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
Sheets("Download").Select
ActiveWorkbook.Queries("Table 0").Delete
End Sub
The following code WORKS, but it's useless because it doesn't reference a variable.
Sub DownloadDataV5()
'
' DownloadDataV5 Macro
Range("B21").Select
Selection.Copy
' comment
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents((""http://insidertrading.org/index.php?sort_by=acceptance_datetime&asc=&symbol=GOOG&date_from=2016-08-03&date_to=2020-12-16&submit=+GO+&page=1"")))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Buy/Sell"", type text}, {""Transaction Date"", type date}, {""Acceptance DateTime"", type" & _
" datetime}, {""Issuer Name"", type text}, {""Issuer Trading Symbol"", type text}, {""Reporting Owner Name"", type text}, {""Reporting Owner Relationship"", type text}, {""Transaction Shares"", Int64.Type}, {""Price per Share"", Currency.Type}, {""Total Value"", Currency.Type}, {""Shares Owned Following Transaction"", Int64.Type}, {""Form"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""" & _
"Changed Type"""
ActiveWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "MySheet"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0"";Extended Properties=""""" _
, Destination:=Range("$A$1")).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
Sheets("Download").Select
ActiveWorkbook.Queries("Table 0").Delete
End Sub
Just need a super quick fix to get this working. Would appreciate all the help I can get!
You're missing the reference to the Weblink variable inside the query's formula
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents((" & Chr(34) & WEBLINK & Chr(34) & ")))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Buy/Sell"", type text}, {""Transaction Date"", type date}, {""Acceptance DateTime"", type" & _
" datetime}, {""Issuer Name"", type text}, {""Issuer Trading Symbol"", type text}, {""Reporting Owner Name"", type text}, {""Reporting Owner Relationship"", type text}, {""Transaction Shares"", Int64.Type}, {""Price per Share"", Currency.Type}, {""Total Value"", Currency.Type}, {""Shares Owned Following Transaction"", Int64.Type}, {""Form"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""" & _
"Changed Type"""
See how I concatenated the variable and added the double quotes here:
" & Chr(34) & WEBLINK & Chr(34) & "
Let me know if it works
The original macro looks like this:
ActiveWorkbook.Queries.Add Name:="Table 0 (2)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://www.reuters.com/companies/luv/financials/income-statement-annual""))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{"""", type text}, {""31-Dec-19"", type number}, {""31-Dec-18"", type number}, {""31-Dec-17"", type text}, {""31-Dec-16"", type text}, {""31-Dec-15"", type tex" & _
"t}, {""Trend"", 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=""Table 0 (2)"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 0 (2)]")
.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__2"
.Refresh BackgroundQuery:=False
Essentially this macro pulls a table from a excel web query from a specific URL (https://www.reuters.com/companies/luv/financials/income-statement-annual). The idea was I wanted to replace luv within the URL with a string variable so I could pull a table from many different sources depending on which URL I wanted to pull data from. I edited my macro to look like this (ticker is the string variable):
ActiveWorkbook.Queries.Add Name:="Table 0 (2)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://www.reuters.com/companies/""&ticker&""/financials/income-statement-annual""))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{"""", type text}, {""31-Dec-19"", type number}, {""31-Dec-18"", type number}, {""31-Dec-17"", type text}, {""31-Dec-16"", type text}, {""31-Dec-15"", type tex" & _
"t}, {""Trend"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
But when I do that I get a Run-time error '424': Object required on this line:
.ListObject.DisplayName = "Table_0__2"
The macro obviously works flawlessly before I attempt to modify the URL with a string variable. And I have checked, the URL's are identical except for (in this case) the name of the company which is what my string variable is supposed to represent. Any advice would be much appreciated.
I am trying to copy data from one excel to the other and then reformat.
This is the code I am using:
ActiveWorkbook.Queries.Add Name:="Export", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""C:\Users\Khawaja\Desktop\Export.csv""),[Delimiter="","", Columns=9, 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"",{{""Name"", type text}, {""Surname"", type" & _
" text}, {""Email"", type text}, {""Action"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Export;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Export]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Export"
.Refresh BackgroundQuery:=False
My source file has data in columns. Each column has a heading of Name, Surname, Email and Action. But when I run the macro, it is not able to detect the column heads.
This is the error I get:
The column 'Name' of the table was not found
Any idea how the error can be removed?
The argument you are looking for in Listobjects.add is XlListObjectHasHeaders. To that argument, you must type XlListObjectHasHeaders:=xlYes. You can also type xlGuess and Excel will guess if the tables has headers. In your case you have string data in both the header and data, therefore the default xlGuess is likely failing for that reason.
ActiveWorkbook.Queries.Add Name:="Export", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""C:\Users\Khawaja\Desktop\Export.csv""),[Delimiter="","", Columns=9, 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"",{{""Name"", type text}, {""Surname"", type" & _
" text}, {""Email"", type text}, {""Action"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Export;Extended Properties=""""" _
, XlListObjectHasHeaders:=xlYes, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Export]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Export"
.Refresh BackgroundQuery:=False
What's wrong with this Excel recorded macro? It returns this error when I try re-running it:
Run-time error '-2147024809 (80070057)':
A query with the name 'Table 0' already exists.
It successfully loads an HTML table from https://www.timeanddate.com/holidays/us/ when I record it.
Code highlighted in yellow by the debugging VBA editor
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://www.timeanddate.com/holidays/us/""))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Header"", type text}, {""Date"", type date}, {""Weekday"", type text}, {""Holiday Name"", type text}, {""Holiday Type"", type text}, {""Where It is Observed"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Full VBA script
Sub readTable()
' readTable Macro
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://www.timeanddate.com/holidays/us/""))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Header"", type text}, {""Date"", type date}, {""Weekday"", type text}, {""Holiday Name"", type text}, {""Holiday Type"", type text}, {""Where It is Observed"", 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=""Table 0"";Extended Properties=""""" _
, Destination:=Range("$A$1")).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
Macro Steps
Selected the Data ribbon
Clicked Get Data > From Other Sources > From Web
Left Basic selected
In the URL field: entered https://www.timeanddate.com/holidays/us/
Clicked Table 0 and Load
The table successfully loads in Sheet2.