我試圖讓Excel返回地址,郵政編碼等之間的行駛距離。我已經使用Google API設置了所有設置,但我不熟悉VBA在所有,所以我不知道這個代碼有什麼問題。VBA代碼不返回大於999的值
每當兩點之間的距離大於999英里時,代碼將只返回第一個數字。所以如果它是1,284英里,它只返回1。如果它是2,817英里,它只返回2,等等。我正在運行的代碼在下面,如果有人可以看看它,告訴我我錯過了什麼,或者我做錯了什麼。
而且我只是儘可能地透明,我自己也沒有寫過這些。我從一個叫做analystcave.com的網站得到了這個信息,我之前在這裏發佈過關於這個問題的信息,而我得到的唯一回應並沒有解決這個問題。我再次發佈了,但我認爲StackOverflow是找到正確答案的更好的地方,而且更快。
非常感謝您的幫助!
'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
您的正則表達式是否在千位分隔符處打破? – steenbergh
我不知道。我如何檢查?對不起,我對VBA一無所知。 – burkamus
我認爲他的意思是,你的輸入文本的正則表達式像10000或10,000像?如果是後者,那麼在處理這個逗號時會遇到問題。 – Mikegrann