简体   繁体   English

VBA代码未返回大于999的值

[英]VBA code not returning values larger than 999

I'm trying get Excel to return the driving distance between to addresses, zip codes, etc. I've got everything setup with the Google API, but I'm not familiar with VBA at all, so I'm not sure what is wrong with this code. 我正在尝试让Excel返回到地址,邮政编码等之间的行驶距离。我已经使用Google API进行了所有设置,但是我对VBA一点都不熟悉,所以我不确定是什么此代码有误。

Whenever the distance between 2 points is greater than 999 miles, the code will only return the first number. 每当两点之间的距离大于999英里时,代码将仅返回第一个数字。 So if it's 1,284 miles, it only returns a 1. If it's 2,817 miles, it only returns a 2, and so forth. 因此,如果是1,284英里,则仅返回1。如果是2,817英里,则仅返回2,依此类推。 The code that I'm running is below if someone could look at it and tell me what I'm missing, or what I'm doing wrong. 如果有人可以看一下并告诉我我所缺少的内容或我做错了什么,那么我正在运行的代码在下面。

And just so I'm being as transparent as possible, I did not write any of this myself. 只是为了让我尽可能的透明,我自己没有写任何内容。 I got it from a site called analystcave.com , and I've posted about this issue on there before, and the only response back I got didn't correct the issue. 我是从一个名为analyticscave.com的网站上获得的 ,并且我之前已经在此网站上发布过有关此问题的信息,而我得到的唯一回复却没有解决该问题。 I've posted again, but I thought StackOverflow was a better place to find the correct answer, and much quicker. 我再次发布,但是我认为StackOverflow是找到正确答案的更好的地方,而且速度更快。

Thank you in advance for your help! 预先感谢您的帮助!

'Calculate Google Maps distance between two addresses
Public Function GetDistance(start As String, dest As String)
    Dim firstVal As String, secondVal As String, lastVal As String

    firstVal = "http://maps.googleapis.com/maps/api/distancematrix/json?origins="

    secondVal = "&destinations="

    lastVal = "&mode=car&language=pl&units=imperial&sensor=false"

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")

    URL = firstVal & Replace(start, " ", "+") & secondVal & Replace(dest, " ", "+") & lastVal

    objHTTP.Open "GET", URL, False

    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

    objHTTP.send ("")

    If InStr(objHTTP.responseText, """distance"" : {") = 0 Then GoTo ErrorHandl

    'Set regex = CreateObject(“VBScript.RegExp”): regex.Pattern = """value"".*?([0-9]+)”: regex.Global = False//
    Set regex = CreateObject("VBScript.RegExp"): regex.Pattern = """text"".*?([0-9]+)": regex.Global = False

    Set matches = regex.Execute(objHTTP.responseText)

    tmpVal = Replace(matches(0).SubMatches(0), ".", Application.International(xlListSeparator))

    Debug.Print matches(0).SubMatches(0)

    GetDistance = CDbl(tmpVal)

    Exit Function
ErrorHandl:
    GetDistance = -1
End Function

The issue is fairly obvious if you look at what is being returned by your request (using inputs "Los Angeles, CA 90001", "New York, NY 10001"): 如果您查看请求返回的内容(使用输入“ Los Angeles,CA 90001”,“ New York,NY 10001”),则问题很明显:

     "elements" : [
        {
           "distance" : {
              "text" : "2 796 mil",
              "value" : 4499875
           },
           "duration" : {
              "text" : "1 dzien 16 godz.",
              "value" : 144859
           },
           "status" : "OK"
        }
     ]

Note that you are requesting the text with a Polish localization here (language= pl ): 请注意,您要在此处索取具有波兰语本地化的文本(language = pl ):

lastVal = "&mode=car&language=pl&units=imperial&sensor=false"

This is localizing the distance text with a space for a thousands separator. 这将距离文本本地化为带有千位分隔符的空间。 Note that the "value" is not localized (and is given in meters) - it gives this when given language=en for the localization and units=metric : 请注意,“值” 本地化(以米为单位)-当给定language=en进行本地化和units=metric时,会给出此值:

     "elements" : [
        {
           "distance" : {
              "text" : "4,500 km",
              "value" : 4499875
           },
           "duration" : {
              "text" : "1 day 16 hours",
              "value" : 144859
           },
           "status" : "OK"
        }
     ]

The quick fix is to change the regex pattern as mentioned in the comments to... 快速解决方案是将注释中提到的正则表达式模式更改为...

regex.Pattern = """text"".*?([0-9,]+)" 

...and the request to: ...以及以下要求:

lastVal = "&mode=car&language=en&units=imperial&sensor=false"

Although I would actually recommend pulling the non-localized "value" instead of the localized "text", and then converting to miles if you need imperial units. 尽管我实际上建议您提取非本地化的“值”而不是本地化的“文本”,然后在需要英制单位时转换为英里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM