Function Similar to importxml in Excel?

元气小坏坏 提交于 2019-11-29 00:41:16

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")

Question is from 2013, some time has passed...

With Excel 2013, there is a function WEBSERVICE to load XML documents, that would do exactly what you want.

There is also FILTERXML to search loaded XML document using XPath.

Function ImportXML(url As String, query As String)

    Dim document    As MSXML2.DOMDocument60
    Dim http        As New MSXML2.XMLHTTP60

    http.Open "GET", url, False
    http.send

    Set document = http.responseXML

    ImportXML = document.SelectSingleNode(query).nodeTypedValue

End Function

The 'From Web' function on the Data menu will pull online data directly into spreadsheet. XML data import is also available under the From Other Sources sub-menu, also listed on the data menu.

Created connections are managed via the Connections dialogue box on the Data menu.

Sample code using record macros while creating a 'From Web' connection:

Sub Macro1()
' Macro1 Macro
With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://en.wikipedia.org/wiki/Microsoft_Excel" _
        , Destination:=Range("$A$1"))
        .Name = _
        "?affID=110195&tt=270912_7a_3912_6&babsrc=HP_ss&mntrId=3e2fc48700000000000088532eb428ec"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!