views:

113

answers:

2

Hi,

I need help creating a VBA macro that downloads closing price data directly from Yahoo Finance's Historic Quotes website and imports the data into an Excel spreadsheet column.

Background information: This is the link to Yahoo Finance's Historical Quote database -

To download as a TXT file: http://ichart.finance.yahoo.com/table.txt?s="StockTicker"&d="EndingMonth"&e="EndingDay"&f="EndingYear"&g=d&a="EndingMonth"&b="EndingDay"&c="EndingYear"&ignore=.txt

Formatting Issue: By default, i.e. using Excel's Web Data Import Wizard, Excel imports the entire table which includes more columns than needed. I am trying to isolate the "close" column. I created a macro that formats the table to isolate the "close" column, but this macro requires me to manually download the data from Yahoo Finance as a txt file:

Sub Test_DownloadTextFile()
    Dim text As String
    'The variables for this URL (in light blue) should be retrieved from the spreadsheet, i.e. "StockTicker" will reference a cell with the ticker symbol "AAPL" in it and that will appear in the URL'
    text = DownloadTextFile("http://ichart.finance.yahoo.com/table.txt?s="StockTicker"&d="EndingMonth"&e="EndingDay"&f="EndingYear"&g=d&a="EndingMonth"&b="EndingDay"&c="EndingYear"&ignore=.txt")

    'At this point I should have the historical quotes table stored in the variable text. How do I select the 4th column and import it into a specific spreadsheet column?'
    Debug.Print text
  End Sub

How can I create a macro that: 1. Refers to the spreadsheet for key variables, e.g. "StockTicker", "EndingMonth", etc.. 2. Downloads the corresponding historic data from Yahoo Finance 3. Imports the data closing price data as a single column into the spreadsheet

I would very much appreciate a practical solution to this problem. Let me know if I need clarify my question or the task at hand. Thank you!

+1  A: 

Check out this SO discussion. Several suggestions that seem worthwhile.

renick
Thank you for this link. I like the idea of storing the txt file in a variable. Is is possible to have a dynamic URL that changes when the user changes the ticker symbol, start and end date, ect? Also, now that the data is stored in a variable, it should be relatively simple to select a specific column and import that column into the spreadsheet, right? Thank you for your help.
AME
+2  A: 

Suggestion: this seems to be the perfect case for a Web Query.
Do you have any reason not to use that ? You can copy just the columns you need afterwards. You did not specify you Excel version, but on 2003 it's on Data/Import External data.

iDevlop
I'm using Excel 2007. The issue with a standard web query is that it will return the entire table and I only need a single column in the table. Is there a way to tweak the web query with VBA?
AME
I don't think so, since you have to select an HTML table. But you could have this web query in a separate sheet and the n either Lookup or Copy/Paste the column you need. If size is critical, you could even have this Web Query in a separate workbook from which you Copy/Paste Value into your main workbook.
iDevlop