Calculate Distance between Zip Codes

mgirvin

Well-known Member
Joined
Dec 15, 2005
Messages
1,245
Office Version
  1. 365
Platform
  1. Windows
Dear Smartest Exclers In The World,

I have a table with row headers as source zip code and column headers as destination zip codes and I would like to calculate the distance between the two zip codes. Does anyone know how to do that, maybe a User Defined function or something?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Sorry, just noticed your location is “Seattle”
I have added a conversion from km to miles ( * 0.62137119)

Start zip code in column A
Finish zip code in column B
Use this formula in column C and copy down

Code:
= ROUND(G_Distance(A2,B2),2)

Put this in module 1

Code:
Function G_DISTANCE(Origin As String, Destination As String) As Double
Dim myRequest As XMLHTTP60
Dim myDomDoc As DOMDocument60
Dim distanceNode As IXMLDOMNode
    G_DISTANCE = 0
    On Error GoTo exitRoute
    Origin = Replace(Origin, " ", "%20")
    Destination = Replace(Destination, " ", "%20")
    Set myRequest = New XMLHTTP60
    myRequest.Open "GET", "http://maps.googleapis.com/maps/api/directions/xml?origin=" _
        & Origin & "&destination=" & Destination & "&sensor=false", False
    myRequest.send
    Set myDomDoc = New DOMDocument60
    myDomDoc.LoadXML myRequest.responseText
    Set distanceNode = myDomDoc.SelectSingleNode("//leg/distance/value")
    If Not distanceNode Is Nothing Then G_DISTANCE = (distanceNode.Text / 1000) * 0.62137119 'convert from km to miles
exitRoute:
    Set distanceNode = Nothing
    Set myDomDoc = Nothing
    Set myRequest = Nothing
End Function
 
Upvote 0
Thanks Taul and SpliierBD!!

When I enter the function, I get a compiling Error. This part of the code is highlighted in yellow: "Function G_DISTANCE(Origin As String, Destination As String) As Double" and this part of the first Dim is highlighted; "myRequest As XMLHTTP60"

Any ideas?
 
Upvote 0
Thanks Taul and SpliierBD!!

When I enter the function, I get a compiling Error. This part of the code is highlighted in yellow: "Function G_DISTANCE(Origin As String, Destination As String) As Double" and this part of the first Dim is highlighted; "myRequest As XMLHTTP60"

Any ideas?


Set a reference to Microsoft XML v6.0

In the VBA editor - Tools>References>Microsoft XML, v6.0

Also, SpillerBD suggests using Google API - He is right, it gives more control and will return the road distance, my method gives straight line distance from the center of the zip codes.
 
Last edited:
Upvote 0
Thank you sir, it’s all about getting the best result, whoever provides it.

I have looked at some downloadable spreadsheets that are out there, the only reasonable one I was able to find was from Juice Analytics called Geocoding Tool v3.1 .xls search for it on line, it’s free.
It uses Yahoo geocoding and requires a Yahoo account, it works great for the US but not very good for the UK, so I abandoned it but it may be useful to you.
There may be a Google maps version but it didn’t exist a few years ago when I was playing with this sort of thing. Or maybe I should say, I couldn’t find one.

Paul.

 
Upvote 0
PS. I think the returned value is a fastest road directions, not as the crow flies.
I compared between directions on the webpage and the macro, and that was the same distance, Which between my TN to WA locations there is no straight road path....
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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