Hi,
I have a sheet named SymbolList in which I list stock symbols in the first column, and have row headings in the first row. I use the following sub to get stock quotes from yahoo. I developed a loop that works in steps of 200 because yahoo has a limit of 200 stock symbols that can be acquired in each request. The loop should go through the whole list in loops of 200 until the last row or if by mistake there is an empty row. The problem I face is that the loop does not work in a consistent manner, for example if I use 200 as the step limit, the sub skips all loops and only gets the last one, if I use a limit of 100 the sub skips the first loop and then proceeds, it only works well at a limit of 50.
I hope someone can help me figure out what is wrong with my loop.
Sub UpdateQuotes()
Dim WSW As Worksheet
Dim Qt As QueryTable
Dim ConnectionString As String, StockFlags As String
Dim i As Integer, iLoopLimit As Integer, LastRow As Integer
StockFlags = "&f=" + "d1t1l1ve1"
DisableUpdate
Set WSW = Worksheets("SymbolList")
LastRow = Range("A65536").End(xlUp).Row
For iLoopLimit = 0 To LastRow Step 200
ConnectionString = "URL;http://download.finance.yahoo.com/d/quotes.csv?s="
i = iLoopLimit + 2
ConnectionString = ConnectionString + Trim(SymbolList.Cells(i, 1).Text)
'i = i + 1
For i = (iLoopLimit + 3) To (iLoopLimit + 51) Step 1
If Cells(i, 1).Text = "" Then
Exit For
Else
ConnectionString = ConnectionString + "+" + Trim(SymbolList.Cells(i, 1).Text)
End If
Next i
ConnectionString = ConnectionString + StockFlags
Set Qt = WSW.QueryTables.Add(Connection:=ConnectionString, Destination:=WSW.Range("H2"))
With Qt
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.TablesOnlyFromHTML = False
End With
With Application
On Error Resume Next
Qt.Refresh BackgroundQuery:=False
End With
WSW.Range("H2:H200").Select
Selection.TextToColumns Destination:=WSW.Range("H2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1))
WSW.Range("H2:M200").Select
Selection.Copy
SymbolList.Cells(iLoopLimit + 2, 2).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Next iLoopLimit
EnableUpdate
End Sub
Bookmarks