patternMinor
Downloading stock information from Yahoo! Finance
Viewed 0 times
downloadingstockyahoofromfinanceinformation
Problem
The program downloads stock information from Yahoo! Finance and displays it in the spreadsheet. On my Mac the program takes 10 minutes to get data for approximately 4000 stocks and on the PC it takes 45 minutes. I wrote another version which copies and pastes the data instead of iterating through each price value, but every time I run that macro, the runtime increases. That version takes about 15 minutes on a PC and 45 minutes on a Mac.
I don't know what is going on but I just want a program that runs in < 15 minutes on both operating systems and has a consistent runtime.
```
Option Explicit
Sub GetData()
Application.Calculation = xlCalculationManual
Dim numb_tickers As Integer
numb_tickers = Application.WorksheetFunction.CountBlank(Worksheets("Sheet1").Range(Sheet1.Cells(2, 2), Sheet1.Cells(5000, 2)))
numb_tickers = 5000 - numb_tickers
Dim start_ticker As Integer
start_ticker = Sheet3.Cells(5, 2) + 1
Dim end_ticker As Integer
end_ticker = Sheet3.Cells(6, 2) + 1
Dim x As Integer
For x = start_ticker To end_ticker
Dim DataSheet As Worksheet
Dim EndDate As Date
Dim StartDate As Date
Dim Symbol As String
Dim qurl As String
Dim nQuery As Name
Dim LastRow As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Data").Cells.Clear
Set DataSheet = ActiveSheet
StartDate = Sheet3.Cells(2, 2)
EndDate = Sheet3.Cells(3, 2)
Symbol = Sheet1.Cells(x, 2)
Sheets("Data").Range("a1").CurrentRegion.ClearContents
qurl = "http://ichart.finance.yahoo.com/table.csv?s=" & Symbol
qurl = qurl & "&a=" & Month(StartDate) - 1 & "&b=" & Day(StartDate) & _
"&c=" & Year(StartDate) & "&d=" & Month(EndDate) - 1 & "&e=" & _
Day(EndDate) & "&f=" & Year(EndDate) & "&g=" & Sheets("Data").Range("a1") & "&q=q&y=0&z=" & _
Symbol & "&x=.csv"
QueryQuote:
With Sheets("Data").QueryTables.Add(Connection:="URL;" & qurl,
I don't know what is going on but I just want a program that runs in < 15 minutes on both operating systems and has a consistent runtime.
```
Option Explicit
Sub GetData()
Application.Calculation = xlCalculationManual
Dim numb_tickers As Integer
numb_tickers = Application.WorksheetFunction.CountBlank(Worksheets("Sheet1").Range(Sheet1.Cells(2, 2), Sheet1.Cells(5000, 2)))
numb_tickers = 5000 - numb_tickers
Dim start_ticker As Integer
start_ticker = Sheet3.Cells(5, 2) + 1
Dim end_ticker As Integer
end_ticker = Sheet3.Cells(6, 2) + 1
Dim x As Integer
For x = start_ticker To end_ticker
Dim DataSheet As Worksheet
Dim EndDate As Date
Dim StartDate As Date
Dim Symbol As String
Dim qurl As String
Dim nQuery As Name
Dim LastRow As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Data").Cells.Clear
Set DataSheet = ActiveSheet
StartDate = Sheet3.Cells(2, 2)
EndDate = Sheet3.Cells(3, 2)
Symbol = Sheet1.Cells(x, 2)
Sheets("Data").Range("a1").CurrentRegion.ClearContents
qurl = "http://ichart.finance.yahoo.com/table.csv?s=" & Symbol
qurl = qurl & "&a=" & Month(StartDate) - 1 & "&b=" & Day(StartDate) & _
"&c=" & Year(StartDate) & "&d=" & Month(EndDate) - 1 & "&e=" & _
Day(EndDate) & "&f=" & Year(EndDate) & "&g=" & Sheets("Data").Range("a1") & "&q=q&y=0&z=" & _
Symbol & "&x=.csv"
QueryQuote:
With Sheets("Data").QueryTables.Add(Connection:="URL;" & qurl,
Solution
I don't know what would cause the large difference in time, but I found a couple minor factors:
Application.ScreenUpdating = Falseis performed in each loop (~4 000 times I assume)
Application.DisplayAlerts = Falseis not only performed in the same volume but also not negated in the scope of this question.
- The lack of
.Value2has a potential performance demerit as discussed here.
Context
StackExchange Code Review Q#98512, answer score: 2
Revisions (0)
No revisions yet.