Calculate Mileage

kshines

Board Regular
Joined
Apr 3, 2011
Messages
56
Office Version
  1. 365
Platform
  1. Windows
Wise people of the forum. I am using the VBA below to calculate the distance between two cities. Currently it sends the results to a pop-up message box. How could it be modified to simply put the results in cell B10 of the same worksheet? It also displays in Meters I would like it in Kilometres


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&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 matches = regex.Execute(objHTTP.responseText)
tmpVal = Replace(matches(0).SubMatches(0), ".", Application.International(xlListSeparator))
GetDistance = CDbl(tmpVal)
Exit Function
ErrorHandl:
GetDistance = -1
End Function


Thanks so much in advance.

KH
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
When posting VBA code, please use the [ code ] and [ / code] tags (minus the spaces) so that it's easier to read. However, after reading your code I don't see where the message box pops up? This seems to just be the function that gets the result. Is there more code?
 
Upvote 0
that's just a function so you would type it directly into the cell or call it in a macro like any other function
 
Upvote 0
Here is the missing VBA you refered to...

Public Function MultiGetDistance(ParamArray args() As Variant) As Double
MultiGetDistance = 0
Dim startLoc As String, endLoc As String, i As Long
For i = LBound(args) To UBound(args) - 1
startLoc = args(i): endLoc = args(i + 1)
MultiGetDistance = MultiGetDistance + GetDistance(startLoc, endLoc)
Next i
End Function

I am trying to get the results to a cell instead of the pop up... hmmm

KH
 
Upvote 0
that is also a function, the macro with the code that calls MsgBox would probably be in a subroutine... it is defined with the keyword Sub...

Code:
Sub SomeMacro()
    MsgBox GetMileage("bla bla", "bla bla")
End Sub

change to...

Code:
Sub SomeMacro()
    Range("B10").Value2 = GetMileage("bla bla", "bla bla")
End Sub

you just have to do the same in your macro
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top