Calculate Distance Between Zip Codes Vba Excel

Posted on

Pdf

From http://analystcave.com/excel-calculate-distances-between-addresses/

Zip Code Distance Formula Excel

Public Function GetDistance(start As String, dest As String)
Dim objHTTP As Object, regex As Object, matches As Object
Dim firstVal As String, secondVal As String, lastVal As String, URL As String, tmpVal As String
firstVal = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins='
secondVal = '&destinations='
lastVal = '&mode=car&language=en&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

'OUTPUT: Distance between the two points in Meters. ' ' ' Calculate geodesic distance (in m) between two points specified by ' latitude/longitude (in numeric decimal degrees) ' using Vincenty inverse formula for ellipsoids ' ' Code has been ported by lostspecies from www.aliencoffee.co.uk to VBA ' from javascript published at. I would like to calculate the distances between postcodes (crow flies would do). Is there a way to do this in Excel. I can list the codes in 2 co. R/excel: Discuss and answer questions about Microsoft Office Excel. Finding the difference in miles between two locations for an Excel spreadsheet. I made a macro myself that just uses zip codes with Bing's API to get distance in Miles and travel time. Would be nice if i.

Codes

This will calculate the distance in meters, traveling by car.

Distance Between Two Zip Codes Excel

Example in a worksheet formula:

Calculate Distance Between Zip Codes Vba Excel

=GetDistance('Trump Tower, New York', 'White House, Washington')/1609.344

to calculate the distance between the two addresses in miles (by car).