I wrote a code to pull a table from a website. The website has a calendar feature and part of the web address can be edited to pull the correct table based on the preferred date. I tried to create a macro that deletes the current table, copies a cell for a new date, opens the code and pastes the date, and then runs the code to populate the new table. However, the macro won't produce the table. Is there a better way to use a macro to edit code, or is there a way to make my code dynamic and change the web address based on the date in the first place?
Code to pull table:
Sub GetWebTable()
Dim URL As String
URL = "http://www.wsj.com/mdc/public/page/2_3023-fut_metal-futures-20170905.html?mod=mdc_pastcalendar"
With ActiveSheet.QueryTables.Add(Connection:="URL;" & URL, Destination:=Range("A1"))
.WebTables = "8"
.Refresh
End With
End Sub
Macro:
Sub Test2()
'
' Test2 Macro
'
'
Columns("A:J").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
Selection.Copy
Application.CutCopyMode = False
Application.Run "Book3!GetWebTable"
Selection.QueryTable.Refresh BackgroundQuery:=False
End Sub
Code to pull table:
Sub GetWebTable()
Dim URL As String
URL = "http://www.wsj.com/mdc/public/page/2_3023-fut_metal-futures-20170905.html?mod=mdc_pastcalendar"
With ActiveSheet.QueryTables.Add(Connection:="URL;" & URL, Destination:=Range("A1"))
.WebTables = "8"
.Refresh
End With
End Sub
Macro:
Sub Test2()
'
' Test2 Macro
'
'
Columns("A:J").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
Selection.Copy
Application.CutCopyMode = False
Application.Run "Book3!GetWebTable"
Selection.QueryTable.Refresh BackgroundQuery:=False
End Sub