我有一个链接"https://maps.googleapis.com/maps/api/distancematrix/xml?origins=..."可以访问以获取 XML 文件数据。
XML 文件:
<DistanceMatrixResponse>
<status>OK</status>
<origin_address>London, UK</origin_address>
<destination_address>Manchester, UK</destination_address>
<row>
<element>
<status>OK</status>
<duration>
<value>14735</value>
<text>4 hours 6 mins</text>
</duration>
<distance>
<value>335534</value>
<text>336 km</text>
</distance>
</element>
</row>
</DistanceMatrixResponse>
XML 文件结构始终相同。我需要获取单元格 A1 和<text>4 hours 6 mins</text>单元格 A2<text>336 km</text>的形式,可以说“联系数据库”。这里的另一个问题是有时可以。我可以用公式来做,但用 VBA 甚至可以吗?4,6336<text>4 hours 6 mins</text><text>1 hour 3 min</text>
我设法使它工作,以便整个 XML 文件数据位于单元格 A1 中。但是无法分离我需要的内容并粘贴到两个不同的单元格。
Sub GoogleAPI1()
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.serverXMLHTTP")
Dim myurl As String
myurl = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" & ThisWorkbook.Worksheets("Contact database").Range("R86").Value _
& "&destinations=" & ThisWorkbook.Worksheets("Contact database").Range("R87").Value & "&mode=" & ThisWorkbook.Worksheets("Contact database").Range("R88").Value _
& "&key=" & ThisWorkbook.Worksheets("Contact database").Range("R82").Value
xmlhttp.Open "GET", myurl, False
xmlhttp.send
ThisWorkbook.Worksheets("Contact database").Range("R92") = xmlhttp.responseText
End Sub