在VBA中解析XML响应并仅提取最后一个数据

时间:2018-09-27 13:12:10

标签: excel vba excel-vba

我正在尝试通过excel vba对内部链接之一(我们公司)进行XML请求。当我使用下面的代码发送请求并接收响应时,得到以下作为响应文本

src="<?php bloginfo('template_directory'); ?>/pageguide-1.3.2/dist/js/pageguide.min.js"

我只想提取最后一个值-即13449。要完成此操作,我需要编写什么代码。

谢谢!!

使用的代码

[{"CPN":"700-42887-01","ExtractDt":"2018-04-02 
00:00:00","Demand":"8645"},"CPN":"700-42887-01","ExtractDt":"2018-04-09 
00:00:00","Demand":"8985"},{"CPN":"700-42887-01","ExtractDt":"2018-04-16 
00:00:00","Demand":"9341"},{"CPN":"700-42887-01","ExtractDt":"2018-04-23 
00:00:00","Demand":"9589"},{"CPN":"700-42887-01","ExtractDt":"2018-04-30 
00:00:00","Demand":"9210"},{"CPN":"700-42887-01","ExtractDt":"2018-05-07 
00:00:00","Demand":"9698"},{"CPN":"700-42887-01","ExtractDt":"2018-05-14 
00:00:00","Demand":"9542"},{"CPN":"700-42887-01","ExtractDt":"2018-05-21 
00:00:00","Demand":"9692"},{"CPN":"700-42887-01","ExtractDt":"2018-05-28 
00:00:00","Demand":"10416"},{"CPN":"700-42887-01","ExtractDt":"2018-06-04 
00:00:00","Demand":"6777"},{"CPN":"700-42887-01","ExtractDt":"2018-06-11 
00:00:00","Demand":"12774"},{"CPN":"700-42887-01","ExtractDt":"2018-06-18 
00:00:00","Demand":"12912"},{"CPN":"700-42887-01","ExtractDt":"2018-06-25 
00:00:00","Demand":"12693"},{"CPN":"700-42887-01","ExtractDt":"2018-07-02 
00:00:00","Demand":"12895"},{"CPN":"700-42887-01","ExtractDt":"2018-07-09 
00:00:00","Demand":"13366"},{"CPN":"700-42887-01","ExtractDt":"2018-07-16 
00:00:00","Demand":"13550"},{"CPN":"700-42887-01","ExtractDt":"2018-07-23 
00:00:00","Demand":"7971"},{"CPN":"700-42887-01","ExtractDt":"2018-07-30 
00:00:00","Demand":"12442"},{"CPN":"700-42887-01","ExtractDt":"2018-08-06 
00:00:00","Demand":"12960"},{"CPN":"700-42887-01","ExtractDt":"2018-08-13 
00:00:00","Demand":"14106"},{"CPN":"700-42887-01","ExtractDt":"2018-08-20 
00:00:00","Demand":"13543"},{"CPN":"700-42887-01","ExtractDt":"2018-08-27 
00:00:00","Demand":"13570"},{"CPN":"700-42887-01","ExtractDt":"2018-09-03 
00:00:00","Demand":"13506"},{"CPN":"700-42887-01","ExtractDt":"2018-09-10 
00:00:00","Demand":"13914"},{"CPN":"700-42887-01","ExtractDt":"2018-09-17 
00:00:00","Demand":"13241"},{"CPN":"700-42887-01","ExtractDt":"2018-09-24 
00:00:00","Demand":"13449"}]

2 个答案:

答案 0 :(得分:1)

考虑到您已经将XML解析为字符串这一事实,那么最简单的事实就是尝试对字符串进行切片。要查看其工作原理,请将字符串从.responseText放入A1范围并运行以下命令:

Sub TestMe()

    Dim responseText As String
    responseText = Range("A1")

    Dim myArr As Variant
    myArr = Split(responseText, "Demand"":""")
    Debug.Print Left(myArr(UBound(myArr)), Len(myArr(UBound(myArr))) - 4)

End Sub

它的作用是用单词Demand":"将字符串分割成数组,并取除数组最后一个单元的最后4个字符以外的任何字符。

答案 1 :(得分:1)

您可以使用InStrRev

Mid$(responseText, InStrRev(responseText, ":") + 2, (InStrRev(responseText, "}") - 1) - (InStrRev(responseText, ":") + 2))

InStrRev从右向左移动字符串。我们知道您想在字符串的末尾使用该值,因此此方向很有用。我们指定要查找的字符作为参数。整个字符串为responseText

要找到的第一个字符是":",从右到左。这将是您拥有:"13449"}]的地方。从此+ 2偏移以获取所需值的实际开始,本例中为1中的13449

确定字符串终点的逻辑相同。我使用"}"作为终点,然后进行调整以前进至数字。 Mid允许您指定字符串,起点和字符数。我传递参数以将所需的字符串提取到Mid。在处理字符串时,我使用类型化的函数(结尾处带有$)更有效。