Related
This is my vba code for powerquery function
ActiveWorkbook.Queries.Add Name:="Or ORder", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""C:\Users\DDK\Downloads\excel.xlsx""), null, true)," & Chr(13) & "" & Chr(10) & " Sheet1_Sheet = Source{[Item=""Sheet1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Column1"", type any}" & _
", {""CROWN"", type any}, {""March 15th, 2021"", type text}, {""Column4"", type any}, {""Column5"", type any}})," & Chr(13) & "" & Chr(10) & " #""Removed Top Rows"" = Table.Skip(#""Changed Type"",2)," & Chr(13) & "" & Chr(10) & " #""Renamed Columns"" = Table.RenameColumns(#""Removed Top Rows"",{{""Column1"", ""QTY""}, {""CROWN"", ""ITEM""}, {""March 15th, 2021"", ""Part""}, {""Column5"", ""Price""}})," & Chr(13) & "" & Chr(10) & " #""Filter" & _
"ed Rows"" = Table.SelectRows(#""Renamed Columns"", each [QTY] <> null and [QTY] <> """")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Filtered Rows"""
ActiveWorkbook.Worksheets.Add.Name = "Our Order"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Or ORder"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Or ORder]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Or_ORder"
.Refresh BackgroundQuery:=False
End With
When the file directory for the source is changed to a variable like this
Dim fileOrder as String
fileOrder = "C:\Users\Ddk\Downloads\excel.xlsx"
....
Source = Excel.Workbook(File.Contents(fileOrder), null, true)," & Chr(13) & "" & Chr(10) ""....
It throws an error when it reaches .Refresh BackgroundQuery:=False. Why is it? Even when it is the exact same string it gives an error [Expression.Error] The import Order matches no exports. Did you miss a module reference?
The thing is I want user to select the excel file for power query through VBA that's why I need to have a variable on file directory.
Why not use VBA to write the path to a named cell range, and have powequery read it in?
let Source = Csv.Document(File.Contents(Excel.CurrentWorkbook(){[Name="RangeName"]}[Content]{0}[Column1]),
or
let Source = Excel.Workbook(File.Contents(Excel.CurrentWorkbook(){[Name="RangeName"]}[Content]{0}[Column1]), null, true),
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'm trying to make my power query macro to work with looping data so I need to replace filepaths and references with variables.
Cell 37,1 has the location of the excel so the macro can run on different computers with simple path edit. The text file is an export from SAP.
I think the problems are in the last references.
Location=EH4
.CommandText = Array("SELECT * FROM [EH4]")
Sub GetDataEH4_2()
Dim FromFolder As String
FromFolder = Cells(37, 1).Value
Dim WorkCenter As String
WorkCenter = "EH4.txt"
Dim WC As String
WC = "EH4"
Dim FilePath As String
FilePath = FromFolder & WorkCenter
ActiveWorkbook.Queries(WC).Delete
ActiveWorkbook.Queries.Add Name:=WC, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""" & FilePath & """),
... Power query stuff, moving columns, removing rows, replacing cell values with "", adding custom row and determing column types ...
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:="OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=EH4;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [EH4]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = WC
.Refresh BackgroundQuery:=False
End With
With this code the error is "Run-time error '1004': [Expression.Error] The value isn't a single-character string."
I've tried changing .CommandText = Array("SELECT * FROM [EH4]") to
"SELECT * FROM" & WC ) but that gives "An operation that uses the database driver could not be completed"
Powerquery stuff here.
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""" & FilePath & """),[Delimiter="" "", Columns=22, Encoding=65001, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Col" & _
"umn6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text}, {""Column14"", type text}, {""Column15"", type text}, {""Column16"", type text}, {""Column17"", type text}, {""Column18"", type text}, {""Column19"", type text}, " & _
"{""Column20"", type text}, {""Column21"", type text}, {""Column22"", type text}})," & Chr(13) & "" & Chr(10) & " #""Removed Top Rows"" = Table.Skip(#""Changed Type"",7)," & Chr(13) & "" & Chr(10) & " #""Removed Columns"" = Table.RemoveColumns(#""Removed Top Rows"",{""Column1"", ""Column3"", ""Column6"", ""Column7"", ""Column10"", ""Column15"", ""Column17"", ""Column18"", ""Column21"", ""Column4""})," & Chr(13) & "" & Chr(10) & " #""Reorder" & _
"ed Columns"" = Table.ReorderColumns(#""Removed Columns"",{""Column20"", ""Column19"", ""Column2"", ""Column5"", ""Column8"", ""Column9"", ""Column11"", ""Column12"", ""Column13"", ""Column14"", ""Column16"", ""Column22""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value"" = Table.ReplaceValue(#""Reordered Columns"",""Total"","""",Replacer.ReplaceText,{""Column2""})," & Chr(13) & "" & Chr(10) & " #""Removed Blank R" & _
"ows"" = Table.SelectRows(#""Replaced Value"", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"""", null})))," & Chr(13) & "" & Chr(10) & " #""Replaced Value1"" = Table.ReplaceValue(#""Removed Blank Rows"",""User op. status"","""",Replacer.ReplaceText,{""Column20""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value2"" = Table.ReplaceValue(#""Replaced Value1"",""Stat"","""",Replacer.ReplaceTex" & _
"t,{""Column19""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value3"" = Table.ReplaceValue(#""Replaced Value2"",""Week"","""",Replacer.ReplaceText,{""Column2""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value4"" = Table.ReplaceValue(#""Replaced Value3"",""Work Ctr"","""",Replacer.ReplaceText,{""Column5""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value5"" = Table.ReplaceValue(#""Replaced Value4"",""Short text for order"","""",Replacer." & _
"ReplaceText,{""Column5""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value6"" = Table.ReplaceValue(#""Replaced Value5"",""Sales ord."","""",Replacer.ReplaceText,{""Column8""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value7"" = Table.ReplaceValue(#""Replaced Value6"",""Op."","""",Replacer.ReplaceText,{""Column9""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value8"" = Table.ReplaceValue(#""Replaced Value7"",""Operation text"","""",Repla" & _
"cer.ReplaceText,{""Column11""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value9"" = Table.ReplaceValue(#""Replaced Value8"",""UserFl"","""",Replacer.ReplaceText,{""Column12""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value10"" = Table.ReplaceValue(#""Replaced Value9"",""User field (20) 1"","""",Replacer.ReplaceText,{""Column13""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value11"" = Table.ReplaceValue(#""Replaced Value10"","" Work""" & _
","""",Replacer.ReplaceText,{""Column14""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value12"" = Table.ReplaceValue(#""Replaced Value11"",""LatestFin."","""",Replacer.ReplaceText,{""Column16""})," & Chr(13) & "" & Chr(10) & " #""Replaced Value13"" = Table.ReplaceValue(#""Replaced Value12"",""Usr date 2"","""",Replacer.ReplaceText,{""Column22""})," & Chr(13) & "" & Chr(10) & " #""Renamed Columns"" = Table.RenameColumns(#""Replaced Value13" & _
""",{{""Column20"", ""System status""}, {""Column19"", ""User status""}, {""Column2"", ""Week""}, {""Column5"", ""Description""}, {""Column8"", ""Sales document""}, {""Column9"", ""Activity""}, {""Column11"", ""Description activity""}, {""Column12"", ""Eng category""}, {""Column13"", ""Engineer name""}, {""Column14"", ""Work hours""}, {""Column16"", ""Latest finish d" & _
"ate""}, {""Column22"", ""Est. finished""}})," & Chr(13) & "" & Chr(10) & " #""Added Custom"" = Table.AddColumn(#""Renamed Columns"", ""Comments"", each """")," & Chr(13) & "" & Chr(10) & " #""Changed Type1"" = Table.TransformColumnTypes(#""Added Custom"",{{""Latest finish date"", type date}, {""Est. finished"", type date}, {""Comments"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type1"""
The same error can come out when trying to record a MACRO to import data to power query.
In my situation, I figured out that the Delimiter="" "" changed to Delimiter=""#(tab)"" solved my problem.
I'll start by admitting Macro's are generally not my thing and writing the code certainly isn't - but in the situation and having read about recording macros i thought it could work for me. If this isn't the way to do it feel free to say so and i will dismiss the idea for the time being until i can do more research and write the code.
Essentially the steps I'm trying to achieve
- Starting from a blank workbook
- Use macro to get data from a folder in our company network containing about 15 workbooks which all have the same internal format and information but relate to different geographic locations
- I recorded the macro using exactly the steps i use if i were doing the task manually which i know works fine
the full code text is below. Upon running i immediately get a run-time error which when debugged highlights the first section and states "A query with the name 'Pipeline Working' already exists. "Pipeline Working" is the name of the folder in which contains the workbooks to import, from there I'm already lost.
Any help appreciated.
Sub PipelinePredictions()
'
' PipelinePredictions Macro
' Import Data from File for Pipeline Predictions
'
'
ActiveWorkbook.Queries.Add Name:="Pipeline Working", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""\\hofileserver\Accounts\ACCOUNTS\Lettings Stats 2018 Qtr 4\Pipelines\Pipeline Working"")," & Chr(13) & "" & Chr(10) & " #""Filtered Hidden Files1"" = Table.SelectRows(Source, each [Attributes]?[Hidden]? <> true)," & Chr(13) & "" & Chr(10) & " #""Invoke Custom Function1"" = Table.AddColumn(#""Filtered Hidden Files1"", ""Transform File from Pipeline Working"", each #""Transform File f" & _
"rom Pipeline Working""([Content]))," & Chr(13) & "" & Chr(10) & " #""Renamed Columns1"" = Table.RenameColumns(#""Invoke Custom Function1"", {""Name"", ""Source.Name""})," & Chr(13) & "" & Chr(10) & " #""Removed Other Columns1"" = Table.SelectColumns(#""Renamed Columns1"", {""Source.Name"", ""Transform File from Pipeline Working""})," & Chr(13) & "" & Chr(10) & " #""Expanded Table Column1"" = Table.ExpandTableColumn(#""Removed Other Columns" & _
"1"", ""Transform File from Pipeline Working"", Table.ColumnNames(#""Transform File from Pipeline Working""(#""Sample File"")))," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Expanded Table Column1"",{{""Source.Name"", type text}, {""Column1"", type any}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type number}" & _
", {""Column6"", Percentage.Type}, {""Column7"", Int64.Type}, {""Column8"", type number}, {""Column9"", type number}, {""Column10"", type number}, {""Column11"", type any}, {""Column12"", type datetime}, {""Column13"", type text}})," & Chr(13) & "" & Chr(10) & " #""Renamed Columns"" = Table.RenameColumns(#""Changed Type"",{{""Source.Name"", ""Branch""}, {""Column1"", ""Week Booked""}, {""Col" & _
"umn2"", ""Neg""}, {""Column3"", ""Property""}, {""Column4"", ""Service Type""}, {""Column5"", ""Rent""}, {""Column6"", ""Percentage Fee""}, {""Column7"", ""Term""}, {""Column8"", ""Fee""}, {""Column9"", ""Admin""}, {""Column10"", ""Total Fees""}, {""Column11"", ""Rent G'tee""}})," & Chr(13) & "" & Chr(10) & " #""Changed Type1"" = Table.TransformColumnTypes(#""Renamed Columns"",{{""Column12" & _
""", type date}})," & Chr(13) & "" & Chr(10) & " #""Renamed Columns2"" = Table.RenameColumns(#""Changed Type1"",{{""Column12"", ""Move in Date""}, {""Column13"", ""Notes""}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Renamed Columns2"""
ActiveWorkbook.Queries.Add Name:="Sample File Parameter1", Formula:= _
"#""Sample File"" meta [IsParameterQuery=true, BinaryIdentifier=#""Sample File"", Type=""Binary"", IsParameterQueryRequired=true]"
ActiveWorkbook.Queries.Add Name:= _
"Transform Sample File from Pipeline Working", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(#""Sample File Parameter1"", null, true)," & Chr(13) & "" & Chr(10) & " #""'A Pipeline$'Print_Area1"" = Source{[Name=""'A Pipeline$'Print_Area""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""'A Pipeline$'Print_Area1"""
ActiveWorkbook.Queries.Add Name:="Sample File", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""\\hofileserver\Accounts\ACCOUNTS\Lettings Stats 2018 Qtr 4\Pipelines\Pipeline Working"")," & Chr(13) & "" & Chr(10) & " Navigation1 = Source{0}[Content]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Navigation1"
ActiveWorkbook.Queries.Add Name:="Transform File from Pipeline Working", _
Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = (#""Sample File Parameter1"") => let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(#""Sample File Parameter1"", null, true)," & Chr(13) & "" & Chr(10) & " #""'A Pipeline$'Print_Area1"" = Source{[Name=""'A Pipeline$'Print_Area""]}[Data]" & Chr(13) & "" & Chr(10) & " in" & Chr(13) & "" & Chr(10) & " #""'A Pipeline$'Print_Area1""" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Pipeline Working"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Pipeline Working]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_ExternalData_1"
.Refresh BackgroundQuery:=False
End With
Workbooks("Book1").Connections.Add2 "Query - Sample File Parameter11", _
"Connection to the 'Sample File Parameter1' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File Parameter1"";Extended Properties=""""" _
, "SELECT * FROM [Sample File Parameter1]", 2
Workbooks("Book1").Connections.Add2 _
"Query - Transform Sample File from Pipeline Working1", _
"Connection to the 'Transform Sample File from Pipeline Working' query in the workbook." _
, Array( _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform Sample File from Pipeline Working"";Extended Propert" _
, "ies="""""), "SELECT * FROM [Transform Sample File from Pipeline Working]", 2
Workbooks("Book1").Connections.Add2 "Query - Sample File1", _
"Connection to the 'Sample File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File"";Extended Properties=""""" _
, "SELECT * FROM [Sample File]", 2
Workbooks("Book1").Connections.Add2 _
"Query - Transform File from Pipeline Working1", _
"Connection to the 'Transform File from Pipeline Working' query in the workbook." _
, _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform File from Pipeline Working"";Extended Properties=""""" _
, "SELECT * FROM [Transform File from Pipeline Working]", 2
Windows("Book3").Activate
Range("A1").Select
End Sub
This opens all workbooks in folder and copies the range called print area into the current spreadsheet - it assumes this code is in a blank workbook
Sub ImportData()
Const FolderToImport = "C:\Pipeline Working\" 'adjust as suits - keep last \ at end
Dim s As String
s = Dir(FolderToImport & "*.xl??")
Do While s <> ""
Dim wb As Workbook
Set wb = Workbooks.Open(FolderToImport & s, False, True)
wb.Sheets(1).Range("print area").Copy Destination:= _
ThisWorkbook.Worksheets(1).Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
wb.Close False
s = Dir()
Loop
End Sub
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.