Está en la página 1de 14

# The Analyst Cave | Excel, VBA, programming and more

 VBA CHEAT SHEET

 VBA TUTORIALS
o EXCEL VBA TUTORIAL
o WORD VBA TUTORIAL
o WEB SCRAPING TUTORIAL
o EXCEL REGEX TUTORIAL
o VBA PERFORMANCE
o VBA CLASS TUTORIAL

 EXCEL TOOLS
o VBA TIME SAVER KIT – CODE SNIPPETS & VBA REFERENCE
o VBA WEB SCRAPING KIT – EASY SCRAPING FOR EXCEL
o VBA COMPILER (TO VB.NET)
o VBA MULTITHREADING TOOL
o EXCEL SCRAPE HTML ADD-IN
 DOCUMENTATION
o GOOGLE CHARTS TOOL
o EXCEL SQL ADD-IN
o EXCEL OPTIMIZER
o HOW TO INSTALL EXCEL ADDINS?

 VBA QUESTIONS?

 CONTACT
Home » Excel VBA Calculate distance between two addresses or
coordinates
EXCEL, GOOGLE API, MS OFFICE, WEB, WEB SCRAPING

False 9 objHTTP. "Mozilla/4. "Mozilla/4. ".International(xlLi 15 GetDistance = CDbl(tmpVal) 16 Exit Function 17 ErrorHandl: GetDistance = -1 18 End Function 19 20 Get Google Maps duration (in seconds) 1 2 Public Function GetDuration(start As String.SubMatches(0).send ("") 12 If InStr(objHTTP. lastVal As String 4 firstVal = "http://maps. " ".Execute(objHTTP.International(xlLi 14 GetDuration = CDbl(tmpVal) 15 Exit Function 16 ErrorHandl: GetDuration = -1 17 End Function 18 19 Calculate distance between two coordinates You can calculate the distance between two coordinates (2 pairs of latitudes and longitudes) using either Google’s API or a simple VBA Function. " ".ServerXMLHTTP") 7 URL = firstVal & Replace(start. secondVal As String.send ("") 11 If InStr(objHTTP. Win 10 objHTTP.Open "GET".".com/maps/api/distancematrix/json?origins=" 5 secondVal = "&destinations=" 6 lastVal = "&mode=car&language=en&sensor=false" Set objHTTP = CreateObject("MSXML2.0 (compatible.".responseText) 14 tmpVal = Replace(matches(0). False 10 objHTTP. Win 11 objHTTP. Application.responseText) 13 tmpVal = Replace(matches(0). """distance"" : {") = 0 Then GoTo ErrorHandl Set regex = CreateObject("VBScript. " ". ". MSIE 6. URL.7 8 URL = firstVal & Replace(start.0.setRequestHeader "User-Agent". Application.Pattern = "duration(?:.SubMatches(0). URL.googleapis.|\n)* 12 Set matches = regex.RegExp"): regex.Pattern = """value"". "+") & secondVal & Replace(dest.Execute(objHTTP. " ".0.setRequestHeader "User-Agent". "+") & secondVal & Replace(dest. MSIE 6.responseText.RegExp"): regex. . dest As String) 3 Dim firstVal As String. """duration"" : {") = 0 Then GoTo ErrorHandl Set regex = CreateObject("VBScript. "+" 8 objHTTP. "+" 9 objHTTP.Open "GET".0 (compatible.*?([0-9 13 Set matches = regex. Calculate distance between coordinates using a VBA function Taking into account the elliptic nature of Mother Earth you can easily calculate the distance between coordinates without using Google API .responseText.

Follow the steps: Add new VBA Module Go to the DEVELOPER ribbon and add select Visual Basic a and new Module to your VBA Project Insert the VBA code Insert the code from sections above (notice that the function is “Public” – therefore will be visible in your workbook as a so called UDF (User Defined Function) Input the function in Excel Go to the worksheet and input the function as shown below: 1 =GetDistance("Chicago".43216. "41.-81. "New York") .43206.Same goes for the duration function: 1 Debug.38992".49992") How to set it up in Excel? Important: Please remember that you need a direct Internet connection! Not via proxy etc.Print GetDuration("41.-81.

endLoc As String. with any coordinates/addresses in between our starting point and our destination.g. i As Long . with your default formula list separator (e. comma in the US)! Calculate distance using Google Maps between any number of coordinates/addresses in Excel Now that we know how to leverage our newly learned GetDistance and GetDuration functions we can use them to measure the distance/duration for any routes. For this I created 2 simple procedures MultiGetDistance and MultiGetDuration: Get distance between any number of locations By using our GetDistance function we can get the distance between any number of locations using the Visual Basic procedure below: 1 Public Function MultiGetDistance(ParamArray args() As Variant) As Double MultiGetDistance = 0 2 Dim startLoc As String.Make sure to replace .

1 startLoc = args(i): endLoc = args(i + 1) 5 MultiGetDistance = MultiGetDistance + GetDistance(startLoc."San Jose") Parameters for calculating the Google Maps Distance The following parameters are available in the API query: PARAM DESCRIPTION Your application’s API key. endLoc) 6 Next i 7 End Function 8 Below example usage: 1 =MultiGetDistance("Chicago"."New York".  walking for pedestrian paths & sidewalks (where available). i As Long 4 For i = LBound(args) To UBound(args) ."New York". . mode (defaults to driving) — Specifies the mode of transport to use when calculating distance Valid values and other request details are specified in the Travel Modes section of this document. Learn how to get a key from the Google Developers Console. This key identifies your application for purposes of quota key management. endLoc) 6 Next i 7 End Function 8 Below example usage: 1 =MultiGetDuration("Chicago". Other modes:  driving (default) for road network."San Jose") And here is the output: Get Google Maps distance between multiple addresses Get duration between any number of locations Similarly by using our GetDuration function we can get the duration between any number of locations using the Visual Basic procedure below: 1 Public Function MultiGetDuration(ParamArray args() As Variant) As Double 2 MultiGetDuration = 0 3 Dim startLoc As String.3 4 For i = LBound(args) To UBound(args) .1 5 startLoc = args(i): endLoc = args(i + 1) MultiGetDuration = MultiGetDuration + GetDuration(startLoc. endLoc As String.