FoxProProgrammer
Programmer
I wrote a macro in VBA for Excel that downloads stock quotes from Yahoo's website and updates a portfolio in a spreadsheet. Yahoo returns the data for each stock ticker as a comma delimted string. For example, if I ask for a quote on Microsoft, the website returns the following string:
MSFT,55.76,"5/16/2002","4:01pm",+0.68,54.71,56.04,54.65,26703600
Where each field is defined as:
Field 1: Ticker symbol
Field 2: Closing price
Field 3: Date of last trade
Field 4: Time of last trade
Field 5: Change for today
Field 6: Open
Field 7: High
Field 8: Low
Field 9: Volume
My program builds a string of multiple stock tickers that it transmits to Yahoo. For example, if I want quotes on five stocks, I build a string and Yahoo returns:
Quote1...9 fields
Quote2...9 fields
Quote3...9 fields
Quote4...9 fields
Quote5...9 fields
where each quote is the same format that I included above.
The web query works fine, and the data is stored in a sheet temporarily while I parse it. I only need the ticker symbol, close, and change, but I can't figure out a way to exclude that from the query to Yahoo's website. Anyway, that isn't the problem; it just creates more data that I have to go through.
I then parse each string and store each field in a cell in another sheet. The parse routine looks for a comma, and uses the Instr and Mid functions to parse each field. Here is the code:
Private Sub Parser()
' Set active sheet to Update
Sheets("Update".Select
Dim i, j, start, position As Integer
Dim celldata, cellsource, celldest, getstr As String
celldata = ""
cellsource = "A1"
celldest = ""
getstr = Range(cellsource).Value
j = 1
' Parse rows and place symbol, last, date, time, and change into cells
Do While getstr <> ""
start = 1
position = 0
' Parse the five fields from each string.
' i = 1 (Symbol)
' i = 2 (Last)
' i = 3 (Date)
' i = 4 (Time)
' i = 5 (Change)
For i = 1 To 5
' Get the position of the next comma
position = InStr(position + 1, getstr, ","
' Parse the data between the last comma and next comma
celldata = Mid(Range(cellsource).Value, start, position - start)
' Store the data in the spreadsheet
Select Case i
Case 1
celldest = "B" & j
Case 2
celldest = "C" & j
Case 3
celldest = "D" & j
Case 4
celldest = "E" & j
Case 5
celldest = "F" & j
End Select
Range(celldest).Value = celldata
' Increment the starting position to look for the next comma
start = position + 1
Next i
j = j + 1
cellsource = "A" & Trim(Str(j))
getstr = Range(cellsource).Value
Loop
End Sub
The parse routine works fine and is very fast. However, the statement that actually writes the parsed string to the cell is EXTREMELY slow. When I run the program on 20 ticker symbols, I can literally count seconds as it places each string in the cell for each ticker symbol. For example, it writes symbol 1, a second or two go by and it writes the last price for symbol 1, a few more seconds go by and it writes the date of the last trade, etc. It takes a minute or two to finish 20 symbols. I don't understand what is going on here to cause such a long delay. I ran in debug mode and determined that the statement that is bogging down the system is:
Range(celldest).Value = celldata
This is the statement that writes the data to the cell.
Does anyone know why this is so slow, or if there's another way to accomplish the same thing that might be faster?
I'm sorry for such a long post, and hope it didn't scare everyone away. The gist of the problem isn't that complicated, but I wanted to provide as much information as might be needed.
Thanks a lot!
dz
MSFT,55.76,"5/16/2002","4:01pm",+0.68,54.71,56.04,54.65,26703600
Where each field is defined as:
Field 1: Ticker symbol
Field 2: Closing price
Field 3: Date of last trade
Field 4: Time of last trade
Field 5: Change for today
Field 6: Open
Field 7: High
Field 8: Low
Field 9: Volume
My program builds a string of multiple stock tickers that it transmits to Yahoo. For example, if I want quotes on five stocks, I build a string and Yahoo returns:
Quote1...9 fields
Quote2...9 fields
Quote3...9 fields
Quote4...9 fields
Quote5...9 fields
where each quote is the same format that I included above.
The web query works fine, and the data is stored in a sheet temporarily while I parse it. I only need the ticker symbol, close, and change, but I can't figure out a way to exclude that from the query to Yahoo's website. Anyway, that isn't the problem; it just creates more data that I have to go through.
I then parse each string and store each field in a cell in another sheet. The parse routine looks for a comma, and uses the Instr and Mid functions to parse each field. Here is the code:
Private Sub Parser()
' Set active sheet to Update
Sheets("Update".Select
Dim i, j, start, position As Integer
Dim celldata, cellsource, celldest, getstr As String
celldata = ""
cellsource = "A1"
celldest = ""
getstr = Range(cellsource).Value
j = 1
' Parse rows and place symbol, last, date, time, and change into cells
Do While getstr <> ""
start = 1
position = 0
' Parse the five fields from each string.
' i = 1 (Symbol)
' i = 2 (Last)
' i = 3 (Date)
' i = 4 (Time)
' i = 5 (Change)
For i = 1 To 5
' Get the position of the next comma
position = InStr(position + 1, getstr, ","
' Parse the data between the last comma and next comma
celldata = Mid(Range(cellsource).Value, start, position - start)
' Store the data in the spreadsheet
Select Case i
Case 1
celldest = "B" & j
Case 2
celldest = "C" & j
Case 3
celldest = "D" & j
Case 4
celldest = "E" & j
Case 5
celldest = "F" & j
End Select
Range(celldest).Value = celldata
' Increment the starting position to look for the next comma
start = position + 1
Next i
j = j + 1
cellsource = "A" & Trim(Str(j))
getstr = Range(cellsource).Value
Loop
End Sub
The parse routine works fine and is very fast. However, the statement that actually writes the parsed string to the cell is EXTREMELY slow. When I run the program on 20 ticker symbols, I can literally count seconds as it places each string in the cell for each ticker symbol. For example, it writes symbol 1, a second or two go by and it writes the last price for symbol 1, a few more seconds go by and it writes the date of the last trade, etc. It takes a minute or two to finish 20 symbols. I don't understand what is going on here to cause such a long delay. I ran in debug mode and determined that the statement that is bogging down the system is:
Range(celldest).Value = celldata
This is the statement that writes the data to the cell.
Does anyone know why this is so slow, or if there's another way to accomplish the same thing that might be faster?
I'm sorry for such a long post, and hope it didn't scare everyone away. The gist of the problem isn't that complicated, but I wanted to provide as much information as might be needed.
Thanks a lot!
dz