【问题标题】:How to debug a macro that calculates distances between two points?如何调试计算两点之间距离的宏?
【发布时间】:2019-04-28 21:38:41
【问题描述】:

我有一个计算两点之间距离的宏。我无法让它工作,需要一些帮助来调试它。

我创建了一个 Google API 密钥,并将其合并到其中,但由于某种原因,该宏不起作用

Public Function

GetDT(origin_city As String, _
origin_state As String, origin_country As String, _
destination_city As String, _
destination_state As String, destination_country As String _
)

Dim surl                As String
Dim oXH                 As Object
Dim bodytxt             As String

Dim distanc_e           As String


surl = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" 
& _
Replace(origin_city, " ", "+") & "+" & Replace(origin_state, " ", "+") & 
"+" & Replace(origin_country, " ", "+") & _
"&destinations=" & _
Replace(destination_city, " ", "+") & "+" & Replace(destination_state, " 
", "+") & "+" & Replace(destination_country, " ", "+") & _
"&mode=driving&units=metric&key=MY_KEY"


Set oXH = CreateObject("msxml2.xmlhttp")

With oXH
    .Open "get", surl, False
    .send
    bodytxt = .responseText
End With

bodytxt = Right(bodytxt, Len(bodytxt) - InStr(1, bodytxt, "<text>") - 5)
tim_e = Left(bodytxt, InStr(1, bodytxt, "</text>") - 1)


bodytxt = Right(bodytxt, Len(bodytxt) - InStr(1, bodytxt, "<text>") - 5)
distanc_e = Left(bodytxt, InStr(1, bodytxt, "</text>") - 1)

GetDT = distanc_e

Set oXH = Nothing

End Function

【问题讨论】:

  • 当你说它不起作用时......它做了什么而不是工作?它是否给出消息、父应用程序是否崩溃、是否发出响亮的哔哔声并从后面喷出烟雾?
  • 给出#VALUE!错误(在我写公式GetDT(......)时在excel界面上,而不是在VBA界面上),而不是2点之间的距离值
  • 因此,当在 VBA 环境中作为函数运行时,它会返回正确的值,但当作为工作表函数输入时,它就不起作用 - 有很多 limitations when using a UDF。我认为Set oXH = CreateObject("msxml2.xmlhttp") 可能属于这一点,但我不确定。

标签: vba google-api xmlhttprequest


【解决方案1】:

使用提供的信息无法自信地回答这个问题。编写一个单独的函数来创建 url 将使您的代码更具可测试性。使用Option Explicit 强制声明所有变量将检测任何拼写错误。

如果 MY_KEY 是一个变量,那么 url 应该类似于 "..metric&amp;key=" &amp; MY_KEY

surl = GetDTURL(origin_city, origin_state, origin_country, destination_city, destination_state, destination_country)
Function GetDTURL(origin_city As String, origin_state As String, origin_country As String, destination_city As String, destination_state As String, destination_country As String)
    Dim surl As String
    Const BaseURl As String = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=@origin_city+@origin_state+@origin_country&destinations=@destination_city+@destination_state+@destination_country&mode=driving&units=metric&key=MY_KEY"
    surl = BaseURl
    surl = Replace(surl, "@origin_city", origin_city)
    surl = Replace(surl, "@origin_state", origin_state)
    surl = Replace(surl, "@origin_country", origin_country)
    surl = Replace(surl, "@destination_city", destination_city)
    surl = Replace(surl, "@destination_state", destination_state)
    surl = Replace(surl, "@destination_country", destination_country)
    surl = Replace(surl, " ", "+")
    GetDTURL = surl
End Function

【讨论】:

  • 感谢@TinMan 在这里付出的努力.....1)MY_KEY 是我的 API 密钥,我没有在这里粘贴......2) 对于你的这行代码- (surl = Replace(" ", "+")),我收到一条错误消息 - 编译错误:参数不是可选的.....您能进一步帮助我吗>?
  • @Varun 谢谢。我更新了我的代码。我应该测试一下这个功能。
  • 我没有得到距离的值,但是这个文本:maps.googleapis.com/maps/api/distancematrix/…SyCF0*EdJoy*****GZ2**********tZQw......你能再帮我一次吗?非常感谢:)
  • @Varun:您在此处提供的链接已损坏,从您的评论中可以看出。另请记住,此地图端点可能不会向读者展示它向您展示的内容,并且它也可能随着时间而改变。因此,您有必要解释它显示了什么以及它有什么问题。最后,最好将这些信息添加到您的问题中。
  • 谢谢,我应该更清楚。您检查的文本消息“maps.googleapis.com/maps/api/distancem.......”是@TinMan 编写的代码的 Excel 输出。链接没有损坏,我更改了密钥,因为我想对其保密。目的是显示生成链接而不是填充距离值。复制我网页上的链接可以提供准确的距离值。如果直接在 Excel 界面上填充该值会很棒,您或 TinMan 是否可以修改他编写的代码以实现这一点?再次感谢
猜你喜欢
  • 2010-10-30
  • 1970-01-01
  • 2011-04-23
  • 2014-11-14
  • 1970-01-01
  • 2011-12-21
相关资源
最近更新 更多