Function Similar to importxml in Excel?

前端 未结 4 875
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 17:50

I love using Google Docs function =importxml() but would love to know if there was anything like it in Excel 2010? I cant seem to find a way for the program to automatically

4条回答
  •  星月不相逢
    2020-12-30 18:20

    You will need to write your own UDF.

    One way would be to use the MSXML2 library, something like this:

    Function GetData(sName As String, sItem As String, Optional sURL = "") As Variant
        Dim oHttp As New MSXML2.XMLHTTP60
        Dim xmlResp As MSXML2.DOMDocument60
        Dim result As Variant
        On Error GoTo EH
    
        If sURL = "" Then
            sURL = "http://util.eveuniversity.org/xml/itemLookup.php?name="
        End If
    
        'open the request and send it'
        oHttp.Open "GET", sURL & sName, False
        oHttp.Send
    
        'get the response as xml'
        Set xmlResp = oHttp.responseXML
        ' get Item'
        GetData = xmlResp.getElementsByTagName(sItem).Item(0).Text
    
        ' Examine output of these in the Immediate window'
        Debug.Print sName
        Debug.Print xmlResp.XML
    
    CleanUp:
        On Error Resume Next
        Set xmlResp = Nothing
        Set oHttp = Nothing
    Exit Function
    EH:
        GetData = CVErr(xlErrValue)
        GoTo CleanUp
    End Function
    

    Call it like this (where A5 contains the required typeName)

    =GetData(A5, "typeID")
    

提交回复
热议问题