使用实时下载创建更新时间序列到excel

时间:2017-10-25 14:15:26

标签: excel excel-vba excel-formula finance yahoo-finance vba

Sub GetData()

Dim QuerySheet As Worksheet
Dim DataSheet As Worksheet
Dim qurl As String
Dim i As Integer
Dim j As Integer
Dim k As Integer

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual

Set DataSheet = ActiveSheet

Range("C7").CurrentRegion.ClearContents
i = 7
qurl = "http://download.finance.yahoo.com/d/quotes.csv?s=" + Cells(i, 1)
i = i + 1
While Cells(i, 1) <> ""
    qurl = qurl + "+" + Cells(i, 1)
    i = i + 1
Wend
qurl = qurl + "&f=" + Range("C2")
Range("c1") = qurl
QueryQuote:
             With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl, 
Destination:=DataSheet.Range("C7"))
                .BackgroundQuery = True
                .TablesOnlyFromHTML = False
                .Refresh BackgroundQuery:=False
                .SaveData = True
            End With

j = Range("A7").End(xlDown).Row

For k = 7 To j

Cells(k, "C").Value = Replace(Cells(k, "C").Value, ", Inc. Common Stoc", "")
Cells(k, "C").Value = Replace(Cells(k, "C").Value, ", Inc. Common St", "")
Cells(k, "C").Value = Replace(Cells(k, "C").Value, ", Inc. Co St", "")
Cells(k, "C").Value = Replace(Cells(k, "C").Value, ", Inc. Co", "")
Cells(k, "C").Value = Replace(Cells(k, "C").Value, ", Inc. (The)", "")
Cells(k, "C").Value = Replace(Cells(k, "C").Value, ", Inc. Com", "")
Cells(k, "C").Value = Replace(Cells(k, "C").Value, ", Inc.", "")
Cells(k, "C").Value = Replace(Cells(k, "C").Value, ", Incorporated C", "")
Cells(k, "C").Value = Replace(Cells(k, "C").Value, ", Incorporated", "")

Next

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


'turn calculation back on
    Application.Calculation = xlCalculationAutomatic
    Application.DisplayAlerts = True
    'Range("C7:H2000").Select
    'Selection.Sort Key1:=Range("C8"), Order1:=xlAscending, Header:=xlGuess,     _
    '    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Columns("C:C").ColumnWidth = 25
    Rows("7:2000").RowHeight = 16
    Columns("J:J").ColumnWidth = 8.5

Call test

End Sub

Sub test()
    Application.OnTime Now + TimeValue("00:00:10"), "GetData"
End Sub

我有这个改编的代码从雅虎财经下载股票价格。我想做的是每次数据刷新时为图形添加值以构建时间序列。每次用新价格刷新单元格然后将它们绘制到图表上时,是否有一种简单的方法可以将每个价格值(20种股票)复制到单独的工作表中?

0 个答案:

没有答案