您可以使用 QueryTables 代替 HTTP Get 调用 (WinHttopRequest),这显然不受 Mac Excel 2011 支持。下面的代码对我有用 - 在列中输入代码,从 A2 开始,输入 yahoo Finance 标签(即 a, b, r, n) 在从 B1 开始的行中。
您可以组装 URL 以调用 csv 的 YF,然后使用 QueryTable 进行调用并将结果粘贴到工作表中。
在 Mac Excel 2011 上工作的代码:
Sub Yahoo_Finance_API_Call_MacExcel2011()
Dim head As Range
Set head = Range("A1")
Dim wb As Workbook 'In the event that you'll use different workbooks
Dim src As Worksheet 'In the event that you'll use different a source worksheet
Dim tgt As Worksheet 'In the event that you'll use different a target worksheet
Set wb = ThisWorkbook
Set src = wb.Sheets("Sheet1")
Set tgt = wb.Sheets("Sheet1")
'Assemble Symbols for API Call
Set rng = Range(head.Offset(1, 0), head.Offset(1, 0).End(xlDown))
For Each cell In rng ' Starting from a cell below the head cell till the last filled cell
Symbols = Symbols & cell.Value & "+"
Next cell
Symbols = Left(Symbols, Len(Symbols) - 1) ' Remove the last '+'
'Assemble Tags or API Call
Set rng = Range(head.Offset(0, 1), head.Offset(0, 1).End(xlToRight))
For Each cell In rng ' Starting from a cell to the right of the head cell till the last filled cell
tags = tags & cell.Value
Next cell
'Build URL
URL = "TEXT;http://finance.yahoo.com/d/quotes.csv?s=" 'Use TEXT to collect API data below
URL = URL & Symbols & "&f=" & tags
'Range("A1").Value = URL 'This will output the assembled URL in a1 for QA if need be
'Call API
With tgt.QueryTables.Add(Connection:= _
URL, _
Destination:=Range(head.Offset(1, 1), head.Offset(1, 1).End(xlDown)))
.RefreshStyle = xlOverwriteCells
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.BackgroundQuery = True
.TextFileCommaDelimiter = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.TextFilePromptOnRefresh = False
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.SaveData = False
End With
End Sub