Calculating Distances between Postcodes

awdl22125

New Member
Joined
Nov 7, 2003
Messages
5
I need to calculate the distances between my company head office and a range of customer addresses. Is there a way of doing this in excel perhaps using google maps?. I am in the UK so the distance needs to be calculated between postcodes.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Googling

"distance between postcodes"

provides a few links that claim to do this.
 
Upvote 0
For addresses, you can use this.

Code:
'A2 = "10 Downing Street, London, SW1A 2AA,United Kingdom"
'B2 = "Countryland, XY14 2LG, United Kingdom"
'C3 = GetDistance(A2, B2) / 1000 '580 in Kilometers

'https://analystcave.com/excel-calculate-distances-between-addresses/
'Returns distance in meters. Divide by 1000 to get kilometers.
'http://www.excelforum.com/showthread.php?t=1140863
Public Function GetDistance(start As String, dest As String)
    Dim firstVal As String, secondVal As String, lastVal As String
    Dim objHTTP As Object, URL As String, RegEx As Object
    Dim matches As Object, tmpVal 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)  'meters
    Exit Function
ErrorHandl:
    GetDistance = -1
End Function

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
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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