So I'm trying to pull closing stock quotes from Alpha Vantage which seems pretty simple. They provide nice API examples. The problem is that my VBA code fails with:
VBAErrorMsg.png
Cut down to a minimum toy example the code below is what I'm trying to do. You'll note that for debug purposes, at one point I save my "qurl" query out to the query sheet. And if I copy/paste that URL into a web browser it works fine so I'm assuming there is no problem in the URL itself, but with the VBA call for the data. Yet the code always crashes pointing to the "Refresh" line. (Changing it to "true" doesn't help - that keeps it from crashing but it spins its wheels indefinitely then.)
Any suggestions to make this run? Thanks.
Bill
Sub AlphaCurrentQuote(Symb As String, AdjCloseQ As Single)
'Retrieve the current closing quote from Alpha Vantage
Dim APIkey As String
Dim QuerySht As Worksheet
Dim DataSht As Worksheet
Dim qurl As String
Set DataSht = ActiveSheet
Set QuerySht = Sheets.Add
APIkey = Range("Notes!C2")
qurl = "http://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=" & Symb & _
"&outputsize=compact&apikey=" & APIkey & "&datatype=csv"
QuerySht.Cells(1, "K") = qurl 'Debug info: Save the url for examination
With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=QuerySht.Range("A1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = False
.Refresh BackgroundQuery:=False
.SaveData = True
End With
Bookmarks