Google Maps Travel Time and Distance Calculator for multiple address

rupertlo

Board Regular
Joined
Sep 10, 2014
Messages
53
I have the following code for calculating the Google Maps distance and times between two address.

Sub GoogleMaps()


Dim myRequest As XMLHTTP60
Dim myDomDoc As DOMDocument60
Dim journey As IXMLDOMNode
Dim RowCount As Integer






RowCount = Worksheets("Calculator").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count - 1


Set myRequest = New XMLHTTP60
myRequest.Open "GET", "http://maps.googleapis.com/maps/api/directions/xml?origin=" _
& Range("A2").Value & "&destination=" & Range("B2").Value & "&sensor=false", False
myRequest.send
Set myDomDoc = New DOMDocument60
myDomDoc.LoadXML myRequest.responseText
Set journey = myDomDoc.SelectSingleNode("//leg/distance/value")
Set Duration = myDomDoc.SelectSingleNode("//leg/duration/value")


' "//leg/duration/value" Duration in seconds ie "14829". Divide by 60 for minutes etc
' "//leg/duration/text" Duration as text ie "4 hours 7 mins"
' "//leg/distance/value" Distance in meters ie "445295". Divide by 1000 for km etc. To convert to miles devide the km by 1.609344.
' "//leg/distance/text" Distance as text ie "277 miles" (can show in km too)
' "//summary" Route Summary ie "I-80-E" or "M25"


Range("C2").Value = Round(journey.Text / 1000 / 1.609344, 0)
Range("D2").Value = Round(Duration.Text / 60, 0)


exitRoute:


Set journey = Nothing
Set Duration = Nothing
Set myDomDoc = Nothing
Set myRequest = Nothing


End Sub

So, in the VBA above, I type the journey start in cell A2, destination in B2. When I execute the code, the distance appears in cell C2 and travel time appears in D2.

I want to be able to do this for a column of start and destination locations in columns A & B to output in columns C & D. I was thinking of doing a loop function (hence the RowCount line). My question is how do you make A2 become A3, B2 become B3 etc etc for each loop? Many thanks.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
give people time to respond to your first post before repeating please
 
Upvote 0
Here's my take:
Code:
Sub GoogleMaps()



Dim myRequest As XMLHTTP60
Dim myDomDoc As DOMDocument60
Dim journey As IXMLDOMNode
Dim RowCount As Integer


Dim c As Range
Dim Rng As Range


Set Rng = Worksheets("GoogleMaps").Range("A:A").Cells.SpecialCells(xlCellTypeConstants)


'RowCount = Worksheets("GoogleMaps").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count - 1


For Each c In Rng


    With c
    
        Set myRequest = New XMLHTTP60
        myRequest.Open "GET", "http://maps.googleapis.com/maps/api/directions/xml?origin=" _
        & .Value & "&destination=" & .Offset(, 1).Value & "&sensor=false", False
        myRequest.send
        Set myDomDoc = New DOMDocument60
        myDomDoc.LoadXML myRequest.responseText
        Set journey = myDomDoc.SelectSingleNode("//leg/distance/value")
        Set Duration = myDomDoc.SelectSingleNode("//leg/duration/value")
        
        
        ' "//leg/duration/value" Duration in seconds ie "14829". Divide by 60 for minutes etc
        ' "//leg/duration/text" Duration as text ie "4 hours 7 mins"
        ' "//leg/distance/value" Distance in meters ie "445295". Divide by 1000 for km etc. To convert to miles devide the km by 1.609344.
        ' "//leg/distance/text" Distance as text ie "277 miles" (can show in km too)
        ' "//summary" Route Summary ie "I-80-E" or "M25"
        
        
        .Offset(, 3).Value = Round(journey.Text / 1000 / 1.609344, 0)
        .Offset(, 4).Value = Round(Duration.Text / 60, 0)
        
    End With
    
exitRoute:
    
    
    Set journey = Nothing
    Set Duration = Nothing
    Set myDomDoc = Nothing
    Set myRequest = Nothing




Next c




End Sub
 
Upvote 0
Thank you Misca...

I am getting a run time error 91 on this line

.Offset(, 3).Value = Round(journey.Text / 1000 / 1.609344, 0)

"Object variable or With block variable not set"
 
Upvote 0
I have a feeling you're getting the error because it doesn't find either the c or one of your locations. There's no error handling on the code so you should check what it is it doesn't find. When you get the error hover your mouse on top of c, journey.Text or Duration.Text to see if they exist.

My code is looking for a sheet named GoogleMaps: I was going to change the "Worksheets("GoogleMaps")" to "Activesheet" but l guess I never did.
 
Upvote 0

Forum statistics

Threads
1,217,997
Messages
6,139,874
Members
450,242
Latest member
mikey18

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