简体   繁体   中英

Excel VBA returning values to multiple cells from XML web query

I have XML data on the web. Those are prices and price data of an in game item:

https://api.eve-central.com/api/marketstat?regionlimit=10000002&typeid=34

Invoke of function to get SINGLE value (for example highest buy order):

     A       B
1 ItemID  buy/max
2  34      =ImportXML("https:// ... &typeid=34", "//buy/max")
3  35

Function looks like:

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

It's OK for one value. If I got 500 items, getting data is extremely long. To populate multiple cells (in this case only two) we need to modify input link by adding &typeid=35 :

https://api.eve-central.com/api/marketstat?regionlimit=10000002&typeid=34&typeid=35

With that link I can get two values into MsgBox . But I need to work on that data so I need that in cell B3 . Now code looks like this:

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

Dim R As Integer
Dim C As Integer
R = ActiveCell.Row                         '<- these variables are for changing'
C = ActiveCell.Column                      '<- output cell'

Set xmlNodeList = document.SelectNodes(query)
For Each xmlNode In xmlNodeList
    Cells(R, C) = xmlNode.nodeTypedValue   '<- THIS LINE CAUSE AN ERROR'
    'MsgBox xmlNode.nodeTypedValue          <- if that line is active I got correct result'
    R = R + 1                                 'but in MsgBox' 
Next xmlNode

End Function

When I'm trying to write data from query into cells there is an problem. What I'm trying to achieve is similar behaviour to ImportXML function from Google Sheets web service . I don't have Excel 2013 so I can not use WEBSERVICE function or FILTERXML and XPath . I have never code in VBA. If You can put here the final code I would be grateful.

For your line giving the error:

Cells(R, C) = xmlNode.nodeTypedValue

Can you try this instead:

Cells(R, C).Value = CStr(xmlNode.nodeTypedValue)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM