直接在excel下载股价

时间:2015-07-05 23:05:36

标签: excel excel-vba vba

是否有一些示例代码可以直接将股价和其他统计数据(尤其是YTD表现)下载到Excel中?

1 个答案:

答案 0 :(得分:3)

几年前,我正在玩这种东西

Public Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long

Public Function getGoogPrice(symbol As String) As Variant
    Dim xmlhttp As Object
    Dim strURL As String
    Dim CompanyID As String
    Dim X As String, Y As Variant
    Dim sSearch As String
    strURL = "http://www.google.com/finance?q=" & symbol
    DeleteUrlCacheEntry (strURL)
    Set xmlhttp = CreateObject("msxml2.xmlhttp")
    With xmlhttp
        .Open "get", strURL, False
        .Send
        X = .ResponseText
    End With
    symbol = UCase(symbol)
    Set xmlhttp = Nothing
    getGoogPrice = Split(Split(Mid(X, InStr(1, X, ":" & UCase(symbol) & """>" & UCase(symbol) & "</a><td class=price>"), 250), ">")(4), "<")(0)
End Function

Public Function getReutersPrice(symbol As String) As Variant
    Dim xmlhttp As Object
    Dim strURL As String
    Dim CompanyID As String
    Dim X As String
    Dim sSearch As String, myDIV As String, myPrice As String

    strURL = "http://www.reuters.com/finance/stocks/overview?symbol=" & symbol 'NESN.VX"
    DeleteUrlCacheEntry (strURL)
    Set xmlhttp = CreateObject("msxml2.xmlhttp")
    With xmlhttp
        .Open "get", strURL, False
        .Send
        X = .ResponseText
    End With
    Set xmlhttp = Nothing
    sSearch = "sectionQuoteDetail"
    myDIV = Mid(X, InStr(1, X, sSearch) + Len(sSearch))
    myDIV = Trim(Mid(myDIV, 1, InStr(1, myDIV, "</div>") - 1))
    Y = Split(myDIV, "</span>")
    myPrice = Mid(Y(1), InStrRev(Y(1), ">") + 1)
    myPrice = Replace(myPrice, Chr(13), "")
    myPrice = Trim(Replace(Replace(myPrice, vbLf, ""), Chr(9), ""))
    getReutersPrice = myPrice
End Function

示例尝试GetReutersPrice(“MSFT”)或GetGoogPrice(“MSFT”)

虽然这会得到当前价格,但您想要的所有内容都应该放在该页面上,并为您正在寻找的Watever进行搜索。