Vba-计算两地间距离及耗时
VBA代码下如:
Sub Main()
Dim strText, URL, CityFrom, CityTo As String
Dim dis, mtime
CityFrom = Sheet1.Range("B2").Value '假想值为江门
CityTo = Sheet1.Range("C2").Value ''假想值为深圳
If CityFrom = "" Or CityTo = "" Then
MsgBox "开始或终点地址为空!"
Else
URL = "http://map.baidu.com/?"
URL = URL & "qt=nav"
URL = URL & "&c=223"
URL = URL & "&sn=2$$$$$$" & CityFrom & "$$0$$$$"
URL = URL & "&en=2$$$$$$" & CityTo & "$$0$$$$"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.Send
strText = .responsetext
dis = Val(Split(strText, """dis"":")(1))
mtime = Val(Mid(strText, InStrRev(strText, """time"":") + 7))
MsgBox "约" & Format(dis / 1000, "0.00公里/") & Format(mtime / 86400, "hh小时mm分钟")
End With
Sheet1.Range("D2").Value = Format(dis / 1000, "0.00公里")
Sheet1.Range("E2").Value = Format(mtime / 86400, "hh:mm:ss")
End If
End Sub
效果图:
- 本文标签: Office
- 本文链接: https://www.jimmy2k.top/article/333
- 版权声明: 本文由JimmyZ的个人博客原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权