Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50070

Exporting Multiple Quotes from Yahoo Finance

$
0
0
Hi all,

I currently have this set of codes that allow me to key in Ticker, Start Date and End Date in the cell "ticker", cell "StartDate" & cell "EndDate" respectively. I did up a macro button that will fetch the data from yahoo finance and place it in a separate sheet.

This are the set of codes:

Code:

Sub GetData()
    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").Range("A:G").Clear
   
    Set DataSheet = ActiveSheet
 
        StartDate = DataSheet.Range("startDate").Value
        EndDate = DataSheet.Range("endDate").Value
        Symbol = DataSheet.Range("ticker").Value
        Sheets("Data").Range("a1").CurrentRegion.ClearContents
       
        qurl = "http://ichart.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=m" & Sheets("Data").Range("a1") & "&q=q&y=0&z=" & _
            Symbol & "&x=.csv"
                 
QueryQuote:
            With Sheets("Data").QueryTables.Add(Connection:="URL;" & qurl, Destination:=Sheets("Data").Range("a1"))
                .BackgroundQuery = True
                .TablesOnlyFromHTML = False
                .Refresh BackgroundQuery:=False
                .SaveData = True
            End With

            Sheets("Data").Range("a1").CurrentRegion.TextToColumns Destination:=Sheets("Data").Range("a1"), DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
                Semicolon:=False, Comma:=True, Space:=False, other:=False

        Sheets("Data").Columns("A:G").ColumnWidth = 12

    LastRow = Sheets("Data").UsedRange.Row - 2 + Sheets("Data").UsedRange.Rows.Count

    With Sheets("Data").Sort
        .SetRange Range("A1:G" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

Now I wish to replicate this and export multiple quotes instead of just one. That is; ticker1 -> data sheet1, ticker2 -> data sheet2, ticker3 -> data sheet3 so on so forth. How do I do it? Which part of the codes do I need to repeat? Appreciate your help!

Viewing all articles
Browse latest Browse all 50070

Trending Articles