我认为,有两种方法可以将yahoo!-Finance数据导入excel。 第一个用于实时数据,第二个用于历史数据。
我需要历史数据。 我当前的VBA代码如下:
firstcolumn = 2 lastcolumn = 6 For n = firstcolumn To lastcolumn Ticker = Worksheets(1).Cells(3, n).Value ActiveWorkbook.Worksheets.Add After:=Worksheets(Worksheets.Count) With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;http://ichart.finance.yahoo.com/table.csv?s=" & Ticker & "&d=" & Month(Date) & "&e=" & Day(Date) & "&f=" & Year(Date) & "&g=d&" _ & "a=" & Month(Date) & "&b=" & Day(Date) & "&c=" & Year(Date) - 1 & "&ignore=.csv" _ , Destination:=Range("$A$1")) .Name = "table.csv?s=BMW.DE&d=6&e=31&f=2012&g=d&a=0&b=1&c=2003&ignore=" .FieldNames = True .RowNumbers = True .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlOverwriteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 850 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(5, 1, 1, 1, 1, 1, 1) .TextFileDecimalSeparator = "." .TextFileThousandsSeparator = "," .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With ActiveSheet.Name = Ticker MsgBox "status ende" ActiveWorkbook.Connections("table.csv?s=" & Ticker & "&d=" & Month(Date) & "&e=" & Day(Date) & "&f=" & Year(Date) & "&g=d&a=" & Month(Date) & "&b=" _ & "" & Day(Date) & "&c=" & Year(Date) - 1 & "&ignore=").Delete ActiveSheet.QueryTables.Item(ActiveSheet.QueryTables.Count).Delete ActiveSheet.ListObjects.Add(xlSrcRange, ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(1, 7).End(xlDown)), , xlYes).Name = Ticker 'MsgBox "The data for " & Ticker & " were downloaded to a new sheet." Next n Exit Sub ERR: MsgBox "Error. Please check."
这将遍历股票行情清单,并创建一个以股票行情清单为名称的新表,并按以下方式下载历史数据:
评论专区