Extract Historical Pricing from Yahoo Finance

  • Before executing the macro enter the ticker symbol in I12 Cell
  • Update the required date,Month,Year in column C and E
  • Date and Seconds auto generated through Event
  •  

    Sub Extract_Historical_Pricing_Data_From_YahooFinance()

    Dim BtnSh As Worksheet
    Set BtnSh = ActiveWorkbook.Sheets("Buttons")

    StartDateVal = BtnSh.Range("M6").Value
    EndDateVal = BtnSh.Range("M8").Value

    Dim OSh As Worksheet
    Set OSh = ActiveWorkbook.Sheets("Output")

    OSh.UsedRange.Clear

    Dim IE As InternetExplorer
    Set IE = New InternetExplorer

    IE.Visible = True
    Ticker = BtnSh.Range("I12").Value


    IE.navigate "https://finance.yahoo.com/quote/" & Ticker & "/history?period1=" & StartDateVal & "&period2=" & EndDateVal & "&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true"


    Application.Wait (Now + TimeValue("00:00:25"))
    Dim doc As HTMLDocument
    Set doc = IE.document
    Application.Wait (Now + TimeValue("00:00:05"))

    IE.StatusBar = True

    Dim tbl
    Set tbl = doc.getElementsByTagName("table")(0)
    RowNumb = 1

    For Each tr In tbl.getElementsByTagName("tr")
    ColNumb = 1
    Application.StatusBar = RowNumb
    For Each td In tr.Children
    If ColNumb = 1 Then
    IE.statusText = td.innerText
    End If
    OSh.Cells(RowNumb, ColNumb).Value = td.innerText
    ColNumb = ColNumb + 1
    Next
    RowNumb = RowNumb + 1
    Next


    IE.Quit
    Set IE = Nothing
    Application.StatusBar = ""
    OSh.Activate

    With OSh.Range(Cells(1, 1), Cells(OSh.UsedRange.Rows.Count - 1, OSh.UsedRange.Columns.Count))
    .Cells.Borders.LineStyle = xlContinuous
    .Cells.Borders.ColorIndex = 1
    .Cells.Borders.Weight = 2
    End With

    With OSh.UsedRange
    .Font.Size = 15
    .HorizontalAlignment = xlLeft
    .VerticalAlignment = xlCenter
    .ColumnWidth = 18

    .Font.Name = "Adobe Garamond Pro Bold"
    With .Rows(1)
    .Interior.ColorIndex = 9
    .Font.ColorIndex = 2
    .Font.Bold = True
    .HorizontalAlignment = xlCenter
    End With

    End With
    MsgBox "Automation Completed"
    End Sub

    Download The Workbook