我在网络上有 XML 数据。这些是游戏内物品的价格和价格数据:
https://api.eve-central.com/api/marketstat?regionlimit=10000002&typeid=34
调用函数以获取SINGLE值(例如最高买入订单):
A B
1 ItemID buy/max
2 34 =ImportXML("https:// ... &typeid=34", "//buy/max")
3 35
函数看起来像:
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
一个值就可以了。如果我有 500 个项目,那么获取数据会非常长。要填充多个单元格(在这种情况下只有两个),我们需要通过添加&typeid=35来修改输入链接:
https://api.eve-central.com/api/marketstat?regionlimit=10000002&typeid=34&typeid=35
通过该链接,我可以将两个值放入MsgBox。但我需要处理这些数据,所以我需要在单元格B3中使用它。现在代码如下所示:
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
当我尝试将查询中的数据写入单元格时,出现了问题。我想要实现的是与Google Sheets web service中的ImportXML函数类似的行为。我没有 Excel 2013,所以我不能使用WEBSERVICE函数或FILTERXML和XPath。我从来没有在 VBA 中编写过代码。如果您可以将最终代码放在这里,我将不胜感激。