Here's a different approach, this is my preference. It uses something called Yahoo Query Language (YQL) which allows you to return data from a wide range of sources (html, XML, Data Tables, JSON, etc) in an XML format using a SQL like syntax. It's a brilliant resource for things like web scraping and getting data in general since it also allows the use of XPath in the Where clause for use on HTML, it also allows you to query many sites in a single call which can be handy.
Anyways, back on topic
You can use YQL to access on-line datatables that have set up, in the example below I am using one of the Yahoo Finance tables called historicaldata, the syntax for the query would be:
You can try it out here to see what it returns:
http://developer.yahoo.com/yql/conso...72012-04-11%27
We can then take the url at the bottom and adapt it to our needs, so something like:
Public Function GetStockProp(strTicker As String, datDay As Date, strProperty) As Double
'YQL Query = select * from yahoo.finance.historicaldata where symbol = '3888.HK' and startDate = '2012-04-11' and endDate = '2012-04-11'
'Test Here: http://developer.yahoo.com/yql/console/?q=select%20*%20from%20yql.query.multi%20where%20queries%3D%22show%20tables%3Bselect%20*%20from%20feed%20where%20url%3D'http%3A%2F%2Fwww.javarants.com%2Frss'%22&env=store://datatables.org/alltableswithkeys#h=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%273888.HK%27%20and%20startDate%20%3D%20%272012-04-11%27%20and%20endDate%20%3D%20%272012-04-11%27
'StrProperty takes one of the follwoing parameters:
' 1. Date
' 2. Open
' 3. High
' 4. Low
' 5. Close
' 6. Volume
' 7. Adj_Close
Dim strDate As String
strProperty = Application.Proper(strProperty)
strDate = Format(datDay, "yyyy-mm-dd")
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20'" _
& strTicker & "'%20and%20startDate%20%3D%20'" _
& strDate & "'%20and%20endDate%20%3D%20'" _
& strDate & "'&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
.send
Do: DoEvents: Loop Until .readystate = 4
If InStr(1, .responseText, "count=""0""") > 0 Then
GetStockProp = 0
Else
GetStockProp = Val(Split(.responseText, "<" & strProperty & ">")(1))
End If
.abort
End With
End Function
It can then be called with something like:
Sub test()
Dim d As Date
d = "2016/4/11"
Debug.Print GetStockProp("3888.HK", d, "close")
End Sub
I've only scratched the surface with what you can do with YQL, but if you are interested in scraping web data, it's well worth looking into.
Bookmarks