Excel vba - yahoo股票期权报价未以csv格式下载

时间:2016-01-04 04:56:50

标签: vba excel-vba csv yahoo-finance stocks

我能够从yahoo.finance获得单个选项引用,数据通过两列(一个标题,一个数据)垂直传递,但我所有尝试水平接收数据的尝试均未成功。我最终试图编写一个命令,下载一系列选项引号,每行将作为一个引用。目前,我甚至无法获得一条水平数据线。请帮忙!

如果有办法与其他免费服务(例如芝加哥期权交易所,谷歌)这样做,请告诉我。任何和所有的帮助将不胜感激!

Sub getOption()
    '
    ' Attempting to return a stock option in a csv format with all datat in either one cell or accross 5 cells in one row
    ' data desired to Retrieve:
    '           name = n
    '           previous close = p
    '           ask = a
    '           bid = b
    '           open interest =  o1
    '
     Const YAHOO_FINANCE_URL = "http://finance.yahoo.com/q/?s=SKX160122C00035000" 'retrieves option data vertically with a header, but adding &f=npbao1, /d/quotes.csv or &e=.csv makes it not work at all. 
    On Error Resume Next
    With ActiveSheet.QueryTables.Add(Connection:="URL;" & YAHOO_FINANCE_URL, Destination:=ActiveCell)
        .Name = "qtActiveRange" & Rnd()
        .RefreshStyle = xlOverwriteCells
        .AdjustColumnWidth = False
        .BackgroundQuery = False
        .Refresh
    End With

End Sub

2 个答案:

答案 0 :(得分:0)

我不确定是否可以一次转置查询,但您可以先将垂直数据放在临时范围内,然后将其水平转置到目标范围。

Range("D4:N5") = WorksheetFunction.Transpose(Range("A4:B14"))

Range("A4:B14")是临时范围。 (我选择了单元格A1并运行getOption

Range("D4:N5")是我假设的目的地范围。

答案 1 :(得分:0)

我不知道SKX是什么,但你可以尝试一下。把你的代码放在ColumnA中,从A7开始,就像这样。

歌 YHOO BIDU IACI MSFT AOL YNDX INSP REDF 新浪 ADBE

然后,运行你的脚本。

Sub GetData()


Dim yahoourl As String
    Dim QuerySheet As Worksheet
    Dim DataSheet As Worksheet
    Dim qurl As String
    Dim i As Integer

    Set DataSheet = ActiveSheet

    i = 7
    yahoourl = "http://quote.yahoo.com/d/quotes.csv?s=" + Cells(i, 1)
    i = i + 1
    While Cells(i, 1) <> ""
        yahoourl = yahoourl + "+" + Cells(i, 1)
        i = i + 1
    Wend
    yahoourl = yahoourl + "&f=" + "l1"

QueryQuote:
             With ActiveSheet.QueryTables.Add(Connection:="URL;" & yahoourl, Destination:=DataSheet.Range("C7"))
                .BackgroundQuery = True
                .TablesOnlyFromHTML = False
                .Refresh BackgroundQuery:=False
                .SaveData = True
            End With

    Application.Calculation = xlCalculationAutomatic
    Application.DisplayAlerts = True
    Columns("C:C").ColumnWidth = 28#
    Cells(2, 3).Select
End Sub