Hi,
Long time reader but just registered to ask my first question.
I am trying to use VBA to pull in historical data from a table on a website, which I have managed to do (see Code 1). However there is a date range that needs to be filled in to state which days the table should show. It is currently limited to the last ~3 months of information. I want to be able to pull in all historical information available via VBA.
To solve this I looked into navigating through the web browser via VBA. I have code that is able to open the URL and then I tried to use the ID to change the attribute value of the beginning of the date range but have limited HTML/web developing experience. Has anyone done this before?
Code1
Pulls in the data from the chart located here: [TABLE="width: 325"]
<tbody>[TR]
[TD]
http://quotes.wsj.com/fx/USDCAD/historical-prices[/TD]
[/TR]
</tbody><colgroup><col></colgroup>[/TABLE]
Sub SearchSite()
strsearch = Range("M5")
theurl = Range("E1")
With ActiveSheet.QueryTables.Add(Connection:="URL;" & theurl, Destination:=Range("I1"))
.Name = "NewsQuery"
.AdjustColumnWidth = False
.TablesOnlyFromHTML = False
.Refresh BackgroundQuery:=True
End With
Debug.Print "DONE"
End Sub
Code 2
Attempts to navigate website and change the 'selectDateFrom' element. Unable to get it to work. Once I get the beginning date to change I think it will be easy to integrate Code 1 into this.
Sub HistoricCADUSA()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate "http://quotes.wsj.com/fx/USDCAD/historical-prices"
'wait for loading
Do While .busy
DoEvents
Loop
Do While .readystate <> 4
DoEvents
Loop
End With
Call ie.document.getelementbyID("{actionform.selectDateFrom}").SetAttribute("Value", "01/01/1990")
End Sub
Long time reader but just registered to ask my first question.
I am trying to use VBA to pull in historical data from a table on a website, which I have managed to do (see Code 1). However there is a date range that needs to be filled in to state which days the table should show. It is currently limited to the last ~3 months of information. I want to be able to pull in all historical information available via VBA.
To solve this I looked into navigating through the web browser via VBA. I have code that is able to open the URL and then I tried to use the ID to change the attribute value of the beginning of the date range but have limited HTML/web developing experience. Has anyone done this before?
Code1
Pulls in the data from the chart located here: [TABLE="width: 325"]
<tbody>[TR]
[TD]
http://quotes.wsj.com/fx/USDCAD/historical-prices[/TD]
[/TR]
</tbody><colgroup><col></colgroup>[/TABLE]
Sub SearchSite()
strsearch = Range("M5")
theurl = Range("E1")
With ActiveSheet.QueryTables.Add(Connection:="URL;" & theurl, Destination:=Range("I1"))
.Name = "NewsQuery"
.AdjustColumnWidth = False
.TablesOnlyFromHTML = False
.Refresh BackgroundQuery:=True
End With
Debug.Print "DONE"
End Sub
Code 2
Attempts to navigate website and change the 'selectDateFrom' element. Unable to get it to work. Once I get the beginning date to change I think it will be easy to integrate Code 1 into this.
Sub HistoricCADUSA()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate "http://quotes.wsj.com/fx/USDCAD/historical-prices"
'wait for loading
Do While .busy
DoEvents
Loop
Do While .readystate <> 4
DoEvents
Loop
End With
Call ie.document.getelementbyID("{actionform.selectDateFrom}").SetAttribute("Value", "01/01/1990")
End Sub