excel - How to make multiple requests to Yahoo Finance to get around the 200 stock limit? -
i have developed excel sheet (with online tutorial) pulls stock information yahoo finance. here code have far:
private sub btnrefresh_click() dim w worksheet: set w = activesheet dim last integer: last = w.range("a1000").end(xlup).row if last = 1 exit sub dim symbols string dim integer = 2 200 symbols = symbols & w.range("a" & i).value & "+" next symbols = left(symbols, len(symbols) - 1) dim url string: url = "http://finance.yahoo.com/d/quotes.csv?s=" & symbols & "&f=sl1w1t8ee8rr5s6j4m6kjp5" dim http new winhttprequest http.open "get", url, false http.send dim resp string: resp = http.responsetext dim lines variant: lines = split(resp, vbnewline) dim sline string = 0 ubound(lines) sline = lines(i) if instr(sline, ",") > 0 values = split(sline, ",") w.cells(i + 2, 4).value = values(1) w.cells(i + 2, 5).value = right(replace(values(2), chr(34), ""), 7) w.cells(i + 2, 7).value = values(3) w.cells(i + 2, 8).value = values(4) w.cells(i + 2, 10).value = values(5) w.cells(i + 2, 11).value = values(6) w.cells(i + 2, 12).value = values(7) w.cells(i + 2, 13).value = values(8) w.cells(i + 2, 14).value = values(9) w.cells(i + 2, 15).value = values(10) w.cells(i + 2, 16).value = values(11) w.cells(i + 2, 17).value = values(12) w.cells(i + 2, 18).value = values(13) end if next w.cells.columns.autofit end sub
the problem having if have more 200 stock tickers in column a, comes error because cannot make request has more 200 ticker symbols. question how can modify code request information first 200 stocks put in data, move next 200 stocks , put in data , on until has gone through every symbol?
this version of function break requests maximum of 100 symbols @ time. results symbols collected resp
before proceeding next phase.
note earlier response has bug: results symbols 200+ overwrite results first batch of symbols.
private sub btnrefresh_click() dim w worksheet: set w = activesheet dim last integer: last = w.range("a1000").end(xlup).row if last = 1 exit sub dim symbols string dim resp string dim integer dim url string dim http winhttprequest resp = "" symbols = "" = 2 last if symbols <> "" symbols = symbols & "+" symbols = symbols & w.range("a" & i).value if mod 100 = 1 or = last ' @ 100 symbols @ time url = "http://finance.yahoo.com/d/quotes.csv?s=" & symbols & "&f=sl1w1t8ee8rr5s6j4m6kjp5" set http = new winhttprequest http.open "get", url, false http.send resp = resp & http.responsetext symbols = "" end if next dim lines variant: lines = split(resp, vbnewline) '' remaining code unchanged
...tom
Comments
Post a Comment