我可以从yahoo.finance获得一个单选报价,数据通过两列垂直传递(一个标题,一个数据),但我所有水平接收数据的尝试都没有成功。我最终试图编写一个命令,下载一个系列选项引号,该引号将以每行一个的形式提供。目前,我甚至无法获得一行水平数据。请帮忙!
如果有其他免费服务(如CBOE、谷歌)可以做到这一点,请告诉我。任何帮助都将不胜感激!
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
我不确定一次转换查询的方法,但您可以先将垂直数据放在临时范围上,然后将其水平转换到目标范围。
Range("D4:N5") = WorksheetFunction.Transpose(Range("A4:B14"))
Range("A4:B14")
是临时范围。(我选择了单元格A1并运行您的getOption
)
Range("D4:N5")
是我假设的目的地范围。
我不知道SKX是什么,但你可以试试这个。把你的股票代码放在A列,从A7开始,类似这样。
GOOGyhoBIDUIACIMSFTAOLYNDXINSPREDF新浪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