+ Reply to Thread
Results 1 to 4 of 4

VB code to compare distances and give me a list of locations Closest to furthest

Hybrid View

  1. #1
    Registered User
    Join Date
    12-07-2011
    Location
    Ontario
    MS-Off Ver
    Excel 2003
    Posts
    20

    VB code to compare distances and give me a list of locations Closest to furthest

    Good day, I currently have a piece of VB code that will get me the distance between 2 cities.

    Thinking I can use this I now need to take the install city/country input (C4) and generate a list out the Depot City/country locations (closest to furthest) based off a sheet of depots that are listed by city/country

    then I need the result list to be displayed in 2 columns (broken out by city F3 and country G4)


    Here is the code I am currently using.


    Option Explicit
    
    'Google Maps Driving Times
    '
    ' ################################################################################
    Sub MTSDistance2()
        Dim dist1 As Double, time1 As Double
        Dim dist2 As Double, time2 As Double
        dist1 = TgetGoogDistanceTime(Range("C4"), Range("E4"), "distance")
        time1 = TgetGoogDistanceTime(Range("C4"), Range("E4"), "time")
    '
        dist2 = VgetGoogDistanceTime(Range("C4"), Range("E4"), "distance")
        time2 = VgetGoogDistanceTime(Range("C4"), Range("E4"), "time")
        MsgBox "Time and Distance from " & Range("C4").Text & " to " & Range("E4").Text & ":" & Chr(10) & _
               "Time = " & Format(Hour(time1), "00") & "h:" & Format(Minute(time1), "00") & "m" & Chr(10) & _
               "Distance = " & Format(dist1 * 1.609344, "0.0") & " Km"
    End Sub
    ' ################################################################################
    'Separate distance and time - text output
    Public Function TgetGoogDistanceTime(rngSAdd As Range, rngEAdd As Range, Optional strReturn As String = "distance") As Variant
        Dim i As Long
        Dim sURL As String
        Dim BodyTxt As String
        Dim vUnits As Variant
        Dim lngDiv As Long
        Dim dblTemp As Double
        sURL = "http://maps.google.com/maps?f=d&source=s_d"
            sURL = sURL & "&saddr=" & Replace(rngSAdd(1).Value, " ", "+")
            sURL = sURL & "&daddr=" & Replace(rngEAdd(1).Value, " ", "+")
            sURL = sURL & "&hl=en"
        BodyTxt = getHTML(sURL)
        If InStr(1, BodyTxt, strReturn, vbTextCompare) = 0 Then
            TgetGoogDistanceTime = "Error"
        Else
            TgetGoogDistanceTime = parseGoog(strReturn, BodyTxt)
            If LCase(strReturn) Like "*time*" Then
                vUnits = Split(TgetGoogDistanceTime)
                For i = LBound(vUnits) To UBound(vUnits) - 1 Step 2
                    dblTemp = dblTemp + _
                            Val(vUnits(i)) / Choose(InStr(1, "hms", Left(vUnits(i + 1), 1), vbTextCompare), 24, 1440, 86400)
                Next i
                TgetGoogDistanceTime = dblTemp
            Else
    '            TgetGoogDistanceTime = Val(TgetGoogDistanceTime)
                TgetGoogDistanceTime = CDbl(Left(TgetGoogDistanceTime, InStr(1, TgetGoogDistanceTime, " ") - 1))
            End If
        End If
    End Function
    
    ' ################################################################################
    'Separate distance and time - not text
    'shred dude vbax
    Public Function VgetGoogDistanceTime( _
            rngSAdd As Range, _
            rngEAdd As Range, _
            Optional strReturn As String = "distance") _
                As Variant
    ' =VGetGoogDistanceTime($A$1,$A$2,"time")
    ' coventry   manchester   02:05
    ' =VGetGoogDistanceTime($A$1,$A$2,"distance")
    ' coventry   manchester   116
    Dim sURL As String
    Dim BodyTxt As String
    sURL = "http://maps.google.com/maps?f=d&source=s_d"
    sURL = sURL & "&saddr=" & Replace(rngSAdd(1).Value, " ", "+")
    sURL = sURL & "&daddr=" & Replace(rngEAdd(1).Value, " ", "+")
    sURL = sURL & "&hl=en"
    BodyTxt = getHTML(sURL)
    If InStr(1, BodyTxt, strReturn, vbTextCompare) = 0 Then
        VgetGoogDistanceTime = "Error"
    Else
        VgetGoogDistanceTime = parseGoog(strReturn, BodyTxt)
        If LCase(strReturn) Like "*time*" Then
            If InStr(1, VgetGoogDistanceTime, "hours", vbTextCompare) <> 0 Then
                VgetGoogDistanceTime = Evaluate("""" & Replace(Replace(Replace(VgetGoogDistanceTime, " hours ", " hour "), " hour ", ":"), " mins", "") & ":0.0" & """+0")
            Else
    '            VgetGoogDistanceTime = Val(VgetGoogDistanceTime)
                VgetGoogDistanceTime = CDbl(Left(VgetGoogDistanceTime, InStr(1, VgetGoogDistanceTime, " ") - 1))
            End If
        Else
            VgetGoogDistanceTime = CDbl(Left(VgetGoogDistanceTime, InStr(1, VgetGoogDistanceTime, " ") - 1))
        End If
    End If
    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
        Else
            parseGoog = Mid(strHTML, InStr(1, strHTML, strSearch) + Len(strSearch))
            parseGoog = Mid(parseGoog, 1, InStr(1, parseGoog, "'") - 1)
        End If
    End Function
    
    
    ' ###################################################################################################################

    Any suggestions

    Thanks in advance
    Last edited by kal-el.Kanata; 12-07-2011 at 02:44 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VB code to compare distances and give me a list of locations Closest to furthest

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    12-07-2011
    Location
    Ontario
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: VB code to compare distances and give me a list of locations Closest to furthest

    Thanks I adjusted as requested, now to get some help with this. I am at a loss
    Last edited by kal-el.Kanata; 12-08-2011 at 02:29 PM.

  4. #4
    Registered User
    Join Date
    12-07-2011
    Location
    Ontario
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: VB code to compare distances and give me a list of locations Closest to furthest

    Is there anyone out there that could help me with this?

+ 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.6.0 RC 1