Mileage Spreadsheet

VPLUMWOOD

New Member
Joined
Jul 13, 2021
Messages
29
Office Version
  1. 365
I am wanting to try and create a mileage claim sheet whereby I can add in the post code of my starting point and end point and excel then works out the miles between - at the minute I do this manually by looking on google maps and seeing what it says on there,

Is there any form of intergration that would be able to do this simply by adding in postcodes?

Thank you
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
There is but you would require an API key from Google, if you google search "Google distance matrix API" you will see what i mean. Once you have the API key and have enabled the distance matrix API via the google cloud console you would be able to use the distance matrix API within Excel VBA.

Google Maps Platform Documentation | Distance Matrix API | Google Developers

This is also possible with software from "HERE technologies" it is almost the same with the exception that HERE needs to convert the postcodes to Lattitude/ Longitude before its distance matrix will provide an answer.

I will provide the code below for a function that returns miles from origin and destination postcode/ city/ POI:
VBA Code:
Public Function GetDistance(start As String, dest As String)
    Dim firstVal As String, secondVal As String, lastVal As String
    Dim apiKEY As String
   
    apiKEY = "API_Key_Goes_Here" 'API key
    firstVal = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="
    secondVal = "&destinations="
    lastVal = "&mode=car&language=en&sensor=false&key=" & apiKEY
    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) / 1609.344 'meters to miles conversion
   
    Exit Function
ErrorHandl:
    GetDistance = "Error"
End Function

The code could be updated somewhat but i just tested it and it is working fine.

Hope this helps
 
Upvote 0

Forum statistics

Threads
1,215,268
Messages
6,123,966
Members
449,137
Latest member
yeti1016

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