Consulting

Page 2 of 3 FirstFirst 1 2 3 LastLast
Results 21 to 40 of 51

Thread: Solved: Total distance and time between two cities in a state

  1. #21
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    All of the driving directions APIs I've found so far require street addresses, and don't take lat/lon pairs. I'd be super happy to hear otherwise.

    By the way MapQuest also has a driving directions API, and their TOS (unlike Google's) doesn't require you to display a map:

    http://www.mapquestapi.com/directions/
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  2. #22
    VBAX Regular
    Joined
    Feb 2011
    Posts
    8
    Location
    OK, I'll check that out, thanks a lot.

  3. #23
    Here you go...

    Attached workbook contains new Class Module to encapsulate functionality.

    Two public Functions in a Module expose methods of the class:

    distCoords, and distAddresses

    Use as formulas from Module:

    =getAddressDistance(...
    =getCoordDistance(stCoords As String, EndCoords As String)
    Attached Files Attached Files
    Last edited by Shred Dude; 02-06-2011 at 01:10 AM.

  4. #24
    VBAX Regular
    Joined
    Feb 2011
    Posts
    8
    Location
    Wow man...it works perfectly...don't know how to thank you...

    The least I could do is put you on the acknowledgment section of the article I'm (hopefully) to publish. I you feel like it don't hesitate to give me your full name and institution of reference.

    Thank you again!

    Regards,

  5. #25
    Hi Guys,

    Can you please help in the code below it is showing me kind of weird error, it was working perfectly untill yesterday but when I tried it today, it is showing me error of every records.

    [vba]
    Public Function getGoogDistanceTime(startAddr As String, startCity As String, _
    startState As String, startZip As String, endAddr As String, _
    endCity As String, endState As String, endZip As String) As String

    Dim sURL As String
    Dim BodyTxt As String

    sURL = "http://maps.google.com/maps?f=d&source=s_d&saddr="
    sURL = sURL & Replace(startAddr, " ", "+") & ",+" & Replace(startCity, " ", "+") & ",+" & startState
    sURL = sURL & "&daddr=" & Replace(endAddr, " ", "+") & ",+" & Replace(endCity, " ", "+") & ",+" & endState
    sURL = sURL & "&hl=en"

    BodyTxt = getHTML(sURL)

    If InStr(1, BodyTxt, "distance:""") = 0 Then getGoogDistanceTime = "Error": Exit Function

    getGoogDistanceTime = parseGoog("distance", BodyTxt) & " / " & parseGoog("time", BodyTxt)

    End Function

    Public Function getHTML(strURL As String) As String
    'Returns the HTML code underlying a given URL
    Dim oXH As Object
    Set oXH = CreateObject("msxml2.xmlhttp")
    With oXH
    .Open "get", strURL, False
    .send
    getHTML = .responseText
    End With
    Set oXH = Nothing
    End Function


    Public Function parseGoog(strSearch As String, strHTML As String) As String
    strSearch = strSearch & ":"""
    If InStr(1, strHTML, strSearch) = 0 Then parseGoog = "Not Found": Exit Function
    parseGoog = Mid(strHTML, InStr(1, strHTML, strSearch) + Len(strSearch))
    parseGoog = Mid(parseGoog, 1, InStr(1, parseGoog, """") - 1)
    End Function
    [/vba]

    Please help me... I tried to work on it but not able to find the issue...
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  6. #26
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    When parsing HTML source code, if your code relies on that code being consistent and that code does change, things go amiss. Looking at the source code, the first change that you might notice is that some double quotes were replaced by single quotes. I have not tried but try:

    [VBA]Private Function parseGoog(strSearch As String, strHTML As String) As String
    strSearch = strSearch & ":'"
    If InStr(1, strHTML, strSearch) = 0 Then parseGoog = "Not Found": Exit Function
    parseGoog = Mid(strHTML, InStr(1, strHTML, strSearch) + Len(strSearch))
    parseGoog = Mid(parseGoog, 1, InStr(1, parseGoog, "'") - 1)
    End Function[/VBA]

  7. #27
    Hi Kenneth,

    Thanks for the suggestion. I tried it but it is still not working and giving me the error plus now the file is hanging and not responding as well.

    I'm attaching the file if in case you might want to test it.

    Looking forward for your reply.


    Thanks
    Attached Files Attached Files
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  8. #28
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    There were too many problems with the one that you posted. I also changed to the WinHttp object and used the previous code modified with the single quote replacements. If the application is volatile then for every recalculation, other functions recalculates so if you have a problem, it is fairly circular and locks Excel. Ctrl+Break and ESC can help and even Application.Calculation=xlCalculationManual in the Immediate window will not stop the code from executing again.

    [VBA]Public Function getGoogDistanceTime(startAddr As String, StartCity As String, _
    startState As String, StartZip As String, endAddr As String, _
    EndCity As String, endState As String, EndZip As String) As String

    Dim sURL As String
    Dim HTTPreq As Object
    Dim BodyTxt As String
    Dim s As String, _
    d As String, _
    t As String

    Application.Volatile False

    sURL = "http://maps.google.com/maps?f=d&source=s_d&saddr="
    sURL = sURL & Replace(startAddr, " ", "+") & ",+" & Replace(StartCity, " ", "+") & ",+" & startState
    sURL = sURL & "&daddr=" & Replace(endAddr, " ", "+") & ",+" & Replace(EndCity, " ", "+") & ",+" & endState
    sURL = sURL & "&hl=en"

    'Set HTTPreq = CreateObject("msxml2.xmlhttp")
    Set HTTPreq = CreateObject("WinHttp.WinHttpRequest.5.1")
    With HTTPreq
    .Open "GET", sURL, False
    .send
    .WaitForResponse
    BodyTxt = .responseText
    End With
    Set HTTPreq = Nothing

    s = "distance:'"

    If InStr(1, BodyTxt, s) = 0 Then getGoogDistanceTime = "Error": Exit Function

    d = Mid(BodyTxt, InStr(1, BodyTxt, s) + Len(s))
    d = Mid(d, 1, InStr(1, d, "'") - 1)

    s = "time:'"
    t = Mid(BodyTxt, InStr(1, BodyTxt, s) + Len(s))
    t = Mid(t, 1, InStr(1, t, "'") - 1)

    getGoogDistanceTime = d & " / " & t

    End Function

    [/VBA]

    See cell I3 and Module 3 for the UDF. I have not worked on the others much to see what they need.
    Attached Files Attached Files

  9. #29
    Thanks a lot Kenneth...for your valuable help and suggestion...
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  10. #30
    VBAX Regular
    Joined
    Feb 2011
    Posts
    8
    Location
    Sorry to bother, but the formula Shred Dude gave me seems to be no longer working. It doesn't return me any result. There has been any change in Google API or it's me?

    I attach you the same file that was working like magic until some days ago. If anyone can help me out will save my life.

    Thanks in advance.
    Attached Files Attached Files

  11. #31
    Hi,

    Just try the code provided by Mr. Kenneth....

    I've attached the sheet provided by him.
    Attached Files Attached Files
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  12. #32
    VBAX Regular
    Joined
    Feb 2011
    Posts
    8
    Location
    Hi,

    Thanks for the tip but the thing is that I need a formula that works with coordinates. Mr. Kenneth formula seems to work ok but it just works with addresses. I really don't get why the formula on the sheet I attached stopped working...

    Please, any help will be welcomed.

    Regards,

  13. #33
    please send the attachment..
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  14. #34
    VBAX Regular
    Joined
    Feb 2011
    Posts
    8
    Location
    It's attached on my last post.

    Regards,

  15. #35
    see if this works for you...
    Attached Files Attached Files
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  16. #36
    VBAX Regular
    Joined
    Feb 2011
    Posts
    8
    Location
    Yeah, works fine! What was the matter then?

    Thanks a lot.

    Regards,

  17. #37
    When parsing HTML source code, if your code relies on that code being consistent and that code does change, things go amiss. Looking at the source code, the first change that you might notice is that some double quotes were replaced by single quotes. I have not tried but try:

    VBA:
    Private Function parseGoog(strSearch As String, strHTML As String) As String
    strSearch = strSearch & ":'"
    If InStr(1, strHTML, strSearch) = 0 Then parseGoog = "Not Found": Exit Function
    parseGoog = Mid(strHTML, InStr(1, strHTML, strSearch) + Len(strSearch))
    parseGoog = Mid(parseGoog, 1, InStr(1, parseGoog, "'") - 1)
    End Function
    Regards,
    Manoj

    "There are no failures - just experiences and your reactions to them."

  18. #38

    many tks

    tks

  19. #39
    This looks to be a very elegant solution.

    Somehow both variations (with Mapquest and Google) are not working on my Excel Application VBA window. I am getting "Error" in case of Google code and "Address Error, fix and try again" for Mapquest.

    In both cases, the internet opens the website and displays the details of Driving directions with distance and time.

    I have the following references in my VBA Editor:

    Microsoft Excel 12.0 Object Library
    Microsoft Internet Controls
    Microsoft Office 12.0 Object Library
    Microsoft Scripting Runtime
    Microsoft HTML Object Library
    Microsoft DAO 3.6 Object Library
    Microsoft VBScript Regular Expressions 5.5

    I am using Excel 2007.
    What could be the problem?

  20. #40

    It's not working...

    It was such a wonderful UDF, since about December, 14. Something changed and it no longer works. If you guys could be amazing again and fix it, I would really really appreciate it!

Posting Permissions

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