2013-08-16 70 views
3

沒有WinHttopRequest上的Excel 2011 Mac版我曾嘗試以下方法,我在其他帖子看到「參考」:WinHttpRequest在Mac OSX上的Excel 2011 VBA

Set HTTP = CreateObject("MSXML2.ServerXMLHTTP") 

,給了我一個「 429'運行時錯誤:ActiveX控制器無法創建對象。

有沒有辦法在Mac Excel上使用WinHttpRequest或類似的東西?我對查詢表也沒有任何好運,並且希望避免這種解決方案。 應該有一個簡單的http GET解決方案,我想這個問題。只是無法找到它的Mac Excel。

我想從雅虎財經API URL數據:

Dim URL As String: URL = "http://finance.yahoo.com/d/quotes.csv?s=" & Symbols & "&f=snl1hg" 
Dim HTTP As New WinHttpRequest 
HTTP.Open "GET", URL, False 
HTTP.Send 

我知道這個工作在Windows,但我使用的是Mac。請指教。謝謝!

+0

你找到了一個解決方案?面對類似的情況。我也想避免QueryTables – ronnyfm

回答

0

我不相信蘋果電腦有什麼等同於MSXML.ServerXMLHTTP。 A suggestion from another stackoverflow thread是使用QueryTables。總之,線程建議:

With ActiveSheet.QueryTables.Add(Connection:="URL;http://carbon.brighterplanet.com/flights.txt", Destination:=Range("A2")) 
    .PostText = "origin_airport=MSN&destination_airport=ORD" 
    .RefreshStyle = xlOverwriteCells 
    .SaveData = True 
    .Refresh 
End With 
+2

我正在尋找更直接的http獲取解決方案。查詢表是真正的唯一方法來獲取數據從雅虎金融在Excel中的MAC?這似乎不太可能。 – brno792

+0

@ brno792你能找到一個最佳的方式來做到這一點? – hax

2

可以代替它顯然不是由Mac的Excel 2011下面的代碼爲我工作支持的HTTP GET調用(WinHttopRequest)的使用QueryTables - 在列中輸入代號,從A2開始,以B1開始的行中輸入雅虎融資標籤(即a,b,r,n)。

您可以組裝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