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

Macro data to a new sheet

$
0
0
I have data input for a particular stock (ticker, start date, end date) on Sheet1. The macro pulls pricing from Yahoo Finance and displays on Sheet1 in Cell C7. What I would like to do is have the data downloaded and displayed on Sheet2 starting in Cell A1.

I'm extremely new to VBA programming so any help is much appreciated. Here is what I have so far...

Code:

Sub GetHistoricalData()
    Dim QuerySheet As Worksheet
    Dim InputSheet As Worksheet
    Dim EndDate As Date
    Dim StartDate As Date
    Dim symbol As String
    Dim qurl As String
    Dim nQuery As Name
 
   
   
 Set InputSheet = ActiveSheet
 
 

   
    StartDate = InputSheet.Range("B2").Value
    EndDate = InputSheet.Range("B3").Value
    symbol = InputSheet.Range("B1").Value
    Range("C7").CurrentRegion.ClearContents

'construct the URL for the query
       
    qurl = "http://chart.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=d" & "&q=q&y=0&z=" & _
        symbol & "&x=.csv"
   
                 
QueryQuote:
    With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=InputSheet.Range("C7"))
        .BackgroundQuery = True
        .TablesOnlyFromHTML = False
        .Refresh BackgroundQuery:=False
        .SaveData = True
    End With
   
    Range("C7").CurrentRegion.TextToColumns Destination:=Range("C7"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=True, Space:=False, other:=False
   
    Range(Range("C7"), Range("C7").End(xlDown)).NumberFormat = "mmm-d-yy"
    Range(Range("D7"), Range("G7").End(xlDown)).NumberFormat = "0.00"
    Range(Range("H7"), Range("H7").End(xlDown)).NumberFormat = "0,000"
    Range(Range("I7"), Range("I7").End(xlDown)).NumberFormat = "0.00"


    With ThisWorkbook
        For Each nQuery In Names
            If IsNumeric(Right(nQuery.Name, 1)) Then
                nQuery.Delete
            End If
        Next nQuery
    End With

'turn calculation back on
    Range("C7:I20000").Select
    Selection.Sort Key1:=Range("C8"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Range("C1:I1").Select
    Selection.ColumnWidth = 10
   
    Set FMV = Range("I9")
    Range("B5") = FMV

End Sub


Viewing all articles
Browse latest Browse all 50103

Trending Articles