+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Getting Distances and DriveTimes

  1. #1
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Getting Distances and DriveTimes

    Getting driving distances and drive times seems to be a very popular topic recently. I had originally posted a thread as to how to get this via the google API which is extremely simple, grabbing this and putting it into excel directly however breaches the terms of use.

    Since then, I have discovered that Microsoft also offers a similar API, you do however need to sign up and get a key (here) and accept their terms of use.

    I don't think posting the below contravenes anything, but if anyone feels otherwise let me know and I'll happily remove this.

    In the below, you need to input your API key in the relevant place

    You'll also need to set a reference to Microsoft XML v6.0

    Function GetDistance(sPCode As String, ePcode As String) As Double
    Dim t As String
    Dim re As XMLHTTP
    
    
    t = "http://dev.virtualearth.net/REST/V1/Routes/Driving?o=xml&wp.0=" & sPCode & "&wp.1=" & ePcode & "&avoid=minimizeTolls&du=mi&key=YOUR_MS_KEY"
    Set re = New XMLHTTP
    
    re.Open "get", t, False
    re.send
    Do
    DoEvents
    Loop Until re.readyState = 4
    
    With re
        s = Split(.responseText, "<TravelDistance>")
    End With
    
    GetDistance = Val(s(1))
    
    End Function
    
    
    Function GetTimeinMins(sPCode As String, ePcode As String) As Double
    Dim t As String
    Dim re As XMLHTTP
    
    t = "http://dev.virtualearth.net/REST/V1/Routes/Driving?o=xml&wp.0=" & sPCode & "&wp.1=" & ePcode & "&avoid=minimizeTolls&du=mi&key=YOUR_MS_KEY"
    
    Set re = New XMLHTTP
    re.Open "get", t, False
    re.send
    Do
    DoEvents
    Loop Until re.readyState = 4
    
    With re
        s = Split(.responseText, "<TravelDuration>")
    End With
    
    GetDistance = Val(s(1)) / 60
    
    End Function
    Last edited by Kyle123; 01-09-2012 at 05:34 PM.
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  2. #2
    Forum Contributor
    Join Date
    03-31-2009
    Location
    Dartford
    MS-Off Ver
    Excel 2007
    Posts
    162

    Re: Getting Distances and DriveTimes

    I have managed to get the API key and I have put this into the cose you have kindley supplied but, I also need the Forumla that needs to go into the cells in excel at present when I was using Google the forumula used was =getGoogDistanceTime(C47,C48) Can you advise what I change this too.

    Thanks

    Shazz

  3. #3
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Getting Distances and DriveTimes

    You just use the name of the function, so =getdistance(a1,a2) etc
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  4. #4
    Registered User
    Join Date
    02-16-2012
    Location
    Illinois
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Getting Distances and DriveTimes

    Thank you for posting this. This is extremely helpful. I got a key and inserted in the code, but I don't think I'm referencing the XML correctly. I try and use the function and I get a compile error [user-defined type not defined], that highlights the "Dim re As XMLHTTP" part of the code

    What am I doing wrong?

    Apologize in advance if this question is basic or ill-defined.

  5. #5
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,954

    Re: Getting Distances and DriveTimes

    Adkf30, welcome to the forum.

    Did you read this bit from the original post: "You'll also need to set a reference to Microsoft XML v6.0"

    In the VB Editor click Tools > References and scroll down the list and select that option.

  6. #6
    Registered User
    Join Date
    02-16-2012
    Location
    Illinois
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Getting Distances and DriveTimes

    Thanks very much Paul. That did the trick and now the GetDistance function works smoothly.

    But when I try and use the GetTimeinMins function I get a:

    "function call on left hand side of assignment must return Variant or Object" error at

    GetDistance = Val(s(1)) / 60

    Ideas on what's happening now?

    Note: I am running Excel 2010, not 2007 as my profile originally stated. Profile has been updated to reflect that.

  7. #7
    Registered User
    Join Date
    02-16-2012
    Location
    Illinois
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Getting Distances and DriveTimes

    Never mind. Figured it out.

    In the line referenced in my previous post, I changed "GetDistance" to "GetTimeinMins" and that did the trick.

    Much appreciate the help. Thx everyone.

  8. #8
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Getting Distances and DriveTimes

    Ah yes, good spot, I'll change it
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  9. #9
    Registered User
    Join Date
    02-24-2012
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Getting Distances and DriveTimes

    Thanks for posting this! After trying to download distance tables from sites that don't really want you to see them, this was a relief.

  10. #10
    Registered User
    Join Date
    04-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Getting Distances and DriveTimes

    Thank you very much for this code and all your advise. I use the distance calculator to report drived miles in my work and this saves me a lot of time.
    Welldone!

  11. #11
    Registered User
    Join Date
    03-12-2012
    Location
    lace, mbeyste
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Getting Distances and DriveTimes

    I don't know what I have done wrong, I get invalid name error in the field where I getdistance in my workbook.

    I made the reference as well as put the code in the VBA editor

  12. #12
    Registered User
    Join Date
    03-12-2012
    Location
    lace, mbeyste
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Getting Distances and DriveTimes

    nevermind, it works flawlessly.


    This really is quite excellent code!

  13. #13
    Registered User
    Join Date
    05-09-2012
    Location
    Brighton UK
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Getting Distances and DriveTimes

    Brilliant! I was looking to do something complicated using a Geomapping program we have...... but now I tried this and everyone will think I am some kind of excel guru!

    Took me a while to work out how to create the key.... I was entering my Bing ID for a while. But I can't blame you for not accounting for idiots!


  14. #14
    Registered User
    Join Date
    05-09-2012
    Location
    Brighton UK
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Getting Distances and DriveTimes

    Hi again..... I have been doing some testing of this code using an extract of all the visits made by our sellers, to calculate total distance travelled.

    Has anyone found any issues where the formula returns a different value than if you go to google maps / get directions? (this is where I think the VB code points to)

    The example if I have is =getdistance(TN23DG,ME24NR)

    My forumula returns 41 miles...... but google maps shows 26 miles? Quite a difference!

    I checked google maps and it does find the exact postcodes when I click on the map location....

    Any ideas anyone - its driving me nuts!

    Kind regards
    Jamie

  15. #15
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Getting Distances and DriveTimes

    The distance returned is in Km not miles And this comes from Microsoft rather than google
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0