Question : Automatic Data Import from Web

Here's the scenario:  I've created an Excel (2003) Spreadsheet which I indend to use for calculating stop-loss points for stock trades based on the "SafeZone" method developed by Dr. Alexander Elder.  I would like to automate data import into this spreadsheet.  The data can be easily downloaded in CSV format from Yahoo Finance.  Here's an example with data for MSFT:

http://finance.yahoo.com/q/hp?s=MSFT&a=05&b=30&c=2003&d=05&e=30&f=2004&g=d

You'll notice the "Download to Spreadsheet" button at the bottom of the page.

This is what I would like to happen:

I would like to create a field at the top of my spreadsheet where I can enter a stock ticker.  I should then be able to click enter or click a button to fetch the data.  Once the data is downloaded in DATE,OPEN,HIGH,LOW,CLOSE,VOLUME format it should be automatically entered into the appropriate location in my spreadsheet.  As my spreadsheet is currently formatted, the first DATE entry should go in cell B7.  I uploaded the SafeZone Spreadsheet to a yahoo group.  I created a temporary account you can use to login when you are prompted:

username:  safez0ne2000           (that's a zero between the z and the n)
password:  password

http://f3.grp.yahoofs.com/v1/8EXjQH6EvQRs8I-JJ5V7NPFpiBH9Tg6QxSNv8C_1_FJAEAiO8piSLwMlTn1ICv2yD19YeuiNr0cMfQRBu42U8GPxE1BZNKP58Ian/SafeZone%20Calculator.xls

Alternately I can email you the file if you like.  

The bottom line is that I would like to only need to enter the ticker symbol and click a button to get this done.  No messing with manually importing the data file.

Thx in advance with your help.

Answer : Automatic Data Import from Web

Dim MyConnection As String
Dim MyStock As String

MyStock =Workbooks("SafeZone Calculator").Worksheets("SafeZone").Range("StockSymbol").Value  /This is assuming you name the cell where you enter the stock symbol "StockSymbol"

MyConnection = "URL; http://finance.yahoo.com/q/hp?s=" & MyStock & "&a=05&b=30&c=2003&d=05&e=30&f=2004&g=d"
/You'll probably want to use the current date as well.  You can do that by  parsing the current date and inserting it into the correct parts of the URL


Set StockInfo = Workbooks("SafeZone Calculator").Worksheets("SafeZone")
Set StockResults = StockInfo.QueryTables _
    .Add(Connection :=MyConnection, _
        Destination := StockInfo.Cells(1,1)) / 1,1 will put the data in the first cell of the first column
With StockResults
    .WebFormatting = xlNone
    .WebSelectionType = xlSpecifiedTables / this means only import the tables you want, in this case, the table with the stock info
    .WebTables = "31" /How did I figure out which table it is?  I used excel's  Create Web Query function, to create the web query, then I saved the query, and
/opened it in a text file.  That told me it was table 31. You shouldn't have to change this unless yahoo changes their site.
    .Refresh
End With


Well, there is the code.  You'll need to create a GUI for it.  A button refering to the macro should sufice.


Random Solutions  
 
programming4us programming4us