• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Get current data from Yahoo

fred3

Member
I have a sub that gets historical data from Yahoo. I want to add some of the current days' data (before closing and well before Yahoo publishes today's data as "historical" around 8 p.m. EST). I believe the codes are: g,h,l1 for Day's Low, Day's High, Last Trade Price and I'm not sure which code would give the Day's Open. ... well, I guess it's o.

The present code works fine. I'd like to add those Yahoo data codes to it but I'm not sure where/how. I'm perfectly happy to have redundant data download if that makes it easier. That is, I can get the historical data each time I get the current data if necessary.

The following code gets historical data into a worksheet "Data".
Date Open High Low Close Volume AdjClose
which, I believe, are data 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
    Dim CurrentFileNameNoExtension As String
'set variable to target the filename=symbol for quotes
CurrentFileNameNoExtension = Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1))
'place file name = symbol in cell B2 without file extension
Sheets("Driver").Select
    Range("I6").Select
ActiveCell.FormulaR1C1 = CurrentFileNameNoExtension
  
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
  
    Sheets("Data").Cells.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.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, 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

    Sheets("Data").Sort.SortFields.Add Key:=Range("A2"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With Sheets("Data").Sort
        .SetRange Range("A1:G" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
        .SortFields.Clear
    End With
    Sheets("Model").Select
    Range("A1").Select
Application.Calculation = xlCalculationAutomatic
End Sub
I realize now that I don't understand the code that says qurl= as it doesn't look like what I might expect.

However, if I create a hyperlink in a worksheet cell like this:
Code:
http://ichart.finance.yahoo.com/table.csv?s=AAPL&f=abcdefqyzoghl1
then I get a .csv file opened in Excel that has all the data I want in one row. Ideally, this would show up in the "Data" worksheet of the current workbook.

So, my question is, how can I modify the code above in order to add columns for Today's Open, Today's Low, Today's High, Today's Last Price in the Data worksheet?
 
Maybe a shorter question:
How do I read this code? i.e. what is it doing in a summarized sense?
Code:
       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, Destination:=Sheets("Data").Range("a1"))
                .BackgroundQuery = True
                .TablesOnlyFromHTML = False
                .Refresh BackgroundQuery:=False
                .SaveData = True
            End With

Well, I can pretty much tell what the qurl lines are doing. But the QueryQuote: statement escapes me!
 
Back
Top