im trying to use excel to say, when i insert stock symbols on column A, it would load me the stock prices...
when i insert till 200 rows, it seems to work
but if say, i insert 400 rows, i starting to get the error: subscript out of range.
test1.JPG
Pls help!
my code:
Sub loaddata()
Dim W As Worksheet: Set W = ActiveSheet
Dim i As Integer
Dim Last As Integer
Worksheets("Stocks").Activate
Last = W.Range("A6000").End(xlUp).Row
If Last = 1 Then Exit Sub
Dim Symbols As String
For i = 2 To Last
Symbols = Symbols & W.Range("A" & i).Value & "+"
Next i
Symbols = Left(Symbols, Len(Symbols) - 1)
Dim URL As String: URL = "http://finance.yahoo.com/d/quotes.csv?s=" & Symbols & "&f=snd1ohgl1v"
Dim X As New WinHttpRequest
X.Open "GET", URL, False
X.send
Dim Resp As String: Resp = X.responseText
Dim Lines As Variant: Lines = Split(Resp, vbCrLf)
Dim sLine As String
Dim Values As Variant
For i = 0 To UBound(Lines)
sLine = Lines(i)
If InStr(sLine, ",") > 0 Then
Values = Split(sLine, ",")
W.Cells(i + 2, 2).Value = Replace(Split(Split(sLine, Chr(34) & "," & Chr(34))(1), Chr(34) & ",")(0), Chr(34), "")
W.Cells(i + 2, 3).Value = Replace(Values(UBound(Values) - 5), Chr(34), "")
W.Cells(i + 2, 3).NumberFormat = "yyyy-mm-dd;@"
W.Cells(i + 2, 4).Value = Values(UBound(Values) - 4)
W.Cells(i + 2, 5).Value = Values(UBound(Values) - 3)
W.Cells(i + 2, 6).Value = Values(UBound(Values) - 2)
W.Cells(i + 2, 7).Value = Values(UBound(Values) - 1)
W.Cells(i + 2, 8).Value = Values(UBound(Values))
End If
Next i
W.Cells.Columns.AutoFit
End Sub
Bookmarks