Free spreadsheet for downloading of Stock Quotes to Excel
This document will show you how to automate the download of the Stock Quotes from Yahoo Finance (http://finance.yahoo.com) to Excel. We will be using a simple programming language called Visual Basic for Applications (VBA) and will be going through the steps, one at a time to allow you to construct the model without prior knowledge of VBA. The completed model is also available for download below.
1. Launch Excel and create a new workbook. Call this workbook AutomatedDownloadData.xls.
In Microsoft Excel 2003: Go to Tools->Macro->Visual Basic Editor
In Microsoft Excel 2007: Go to Developer->Visual Basic
2. Right click on VBAProject (your Excel Filename) and select Insert->Module. A default Module1 will be inserted. Click on Module1 and paste the VBA code below on the right hand side of the Visual Basic Editor. The following VBA code allows you to download data from Yahoo Finance using a specified Stock symbol, Start Date and End Date.
Sub GetStock(ByVal stockSymbol As String,
ByVal StartDate As Date, ByVal EndDate As Date)
Dim DownloadURL As String
Dim StartMonth, StartDay, StartYear As String
Dim EndMonth, EndDay, EndYear As String
StartMonth = Format(Month(StartDate)-1, "00")
StartDay = Format(Day(StartDate), "00")
StartYear = Format(Year(StartDate), "00")
EndMonth = Format(Month(EndDate)-1, "00")
EndDay = Format(Day(EndDate), "00")
EndYear = Format(Year(EndDate), "00")
DownloadURL="URL;http://table.finance.yahoo.com/table.csv?s="
+ stockSymbol
+ "&a=" + StartMonth + "&b="
+ StartDay + "&c=" + StartYear
+ "&d=" + EndMonth + "&e="
+ EndDay + "&f="
+ EndYear + "&g=d&ignore=.csv"
With ActiveSheet.QueryTables.Add(Connection:=DownloadURL,
Destination:=Range("$A$1"))
.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
.WebTables = "20"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
ActiveWindow.SmallScroll Down:=-12
Columns("A:A").TextToColumns Destination:=Range("A1"),_
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), Array(6, 1), Array(7, 1))
Columns("A:F").EntireColumn.AutoFit
End Sub
Sub Download()
Call GetStock("YHOO", "02/01/2007", "09/05/2008")
End Sub
3. Click on Save and then File->Close to return to Microsoft Excel. In Excel, try out the downloading of data by going to
Excel 2003: Go to Tools->Macro->Macros…, select the Download macro and click run
Excel 2007: Go to Developer->Macros, select the Download macro and click run.
and press the Enter key. This will initiate a download of Stock Prices from Yahoo! Finance.
What we have written is a subroutine that downloads stock data automatically and can be executed by Excel. It takes "stockSymbol", "StartDate" and "EndDate" as parameters.
In the whole block of code above, the most important line is the following.
With ActiveSheet.QueryTables.Add(Connection:=DownloadURL,
Destination:=Range("$A$1"))
It basically says that we will be downloading data from DownloadURL and placing the result into the cell A1. The DownloadURL is constructed based on the parameters explained below.
http://table.finance.yahoo.com/table.csv?s=YHOO&a=01&b=01&c=2007&d=08&e=05&f=2008&g=d&ignore=.csv
- “s=YHOO” means to download the stock prices of Yahoo. YHOO is the stock symbol of Yahoo.
- “a=01&b=01&c=2007” specifies the start date in Month, Day, Year. You might have noticed that the month is subtracted with 1, which is the format required by Yahoo.
- “d=08&e=05&f=2008” specifies the end date in Month, Day, Year. You might have noticed that the month is subtracted with 1, which is the format required by Yahoo.
You can also find the implementation of this example in the workbook DownloadStockQuotes.xls.
Download
Free spreadsheet to download Stock Quotes - v1.0
System Requirements
Microsoft® Windows® 2000 ,Microsoft® Windows XP® and 2003, Microsoft® Windows Vista®
512 MB RAM
5 MB of Hard Disk space
Excel 2002, 2003, XP or Excel 2007
License
By downloading this software from our web site, you agree to the terms of our license agreement.
Download
DownloadStockQuotes.zip (Zip Format - 231 KB)
|