+ Reply to Thread
Results 1 to 8 of 8

Distances betwteen two zip codes

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-18-2010
    Location
    Walla Walla, Washington
    MS-Off Ver
    Excel 2007
    Posts
    124

    Distances betwteen two zip codes

    I am looking for a way to reference a zip code of a customer on my estimate sheet against the zip code I life in to determine the approximate distance of the job. Thanks for any help in advance.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Distances betwteen two zip codes

    You would probably need to have a helper sheet with each zip code listed and the approximate distance from where you live. You could then use a lookup function to display it on your estimate sheet.

  3. #3
    Forum Contributor
    Join Date
    10-18-2010
    Location
    Walla Walla, Washington
    MS-Off Ver
    Excel 2007
    Posts
    124

    Re: Distances betwteen two zip codes

    Do you think there is a way to do this using a reference through google maps/earth or mapquest?

  4. #4
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Distances betwteen two zip codes

    This function uses Google Maps (not the API) and returns the distance between 2 addresses. It can called from VBA or as a worksheet cell function. For example with the start address in A1 and the end address in B1, in C1 enter =GMaps_Distance(A1,B1). The addresses can be any address, location, latitude and longitude coordinates, zip code, post code, etc.
    Public Function GMaps_Distance(FromAddress As String, ToAddress As String) As String
    
        Static XML As Object
        Dim URL As String
        Dim HTMLdoc As Object
        Dim routeLI As Object
    
        URL = "http://maps.google.com/maps?f=d&source=s_d&saddr=" & Escape(FromAddress) & "&daddr=" & Escape(ToAddress)
        
        If XML Is Nothing Then Set XML = CreateObject("MSXML2.XMLHTTP")
        With XML
            .Open "GET", URL, False
            .send
            Set HTMLdoc = New HTMLDocument
            HTMLdoc.body.innerHTML = .responseText
        End With
            
        GMaps_Distance = ""
        Set routeLI = HTMLdoc.getElementById("altroute_0")
        If Not routeLI Is Nothing Then
            GMaps_Distance = Left(routeLI.innerText, InStr(routeLI.innerText, ", ") - 1)
        End If
    
    End Function
    
    
    Private Function Escape(ByVal paramValue As String) As String
    
        Dim i As Integer, BadChars As String
    
        BadChars = "<>%=&!@#$^()+{[}]|\;:'"",/?"
        For i = 1 To Len(BadChars)
            paramValue = Replace(paramValue, Mid(BadChars, i, 1), "%" & Hex(Asc(Mid(BadChars, i, 1))))
        Next
        paramValue = Replace(paramValue, " ", "+")
        Escape = paramValue
    
    End Function
    Post responsibly. Search for excelforum.com

  5. #5
    Forum Contributor
    Join Date
    10-18-2010
    Location
    Walla Walla, Washington
    MS-Off Ver
    Excel 2007
    Posts
    124

    Re: Distances betwteen two zip codes

    The macro seems to get hinged up at

    Set HTMLdoc = New HTMLDocument

    Any suggestions?

  6. #6
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Distances betwteen two zip codes

    Oops! Change that line to:
            Set HTMLdoc = CreateObject("HTMLFile")

  7. #7
    Forum Contributor
    Join Date
    10-18-2010
    Location
    Walla Walla, Washington
    MS-Off Ver
    Excel 2007
    Posts
    124

    Re: Distances betwteen two zip codes

    There is still an issue. not sure where. Do you mind taking another look? Thanks. Merry Christmas everyone.

  8. #8
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Distances betwteen two zip codes

    What is the issue? Does it display an error? Is the distance returned blank or incorrect? Here is the complete code again, including a test routine which displays "2,764 mi" for me.
    Sub Test_Distance()
        'Microsoft HQ to White House
        MsgBox GMaps_Distance("98052", "20500")
    End Sub
    
    
    Public Function GMaps_Distance(FromAddress As String, ToAddress As String) As String
    
        Static XML As Object
        Dim URL As String
        Dim HTMLdoc As Object
        Dim routeLI As Object
    
        URL = "http://maps.google.com/maps?f=d&source=s_d&saddr=" & Escape(FromAddress) & "&daddr=" & Escape(ToAddress)
        
        If XML Is Nothing Then Set XML = CreateObject("MSXML2.XMLHTTP")
        With XML
            .Open "GET", URL, False
            .send
            Set HTMLdoc = CreateObject("HTMLFile")
            HTMLdoc.body.innerHTML = .responseText
        End With
            
        GMaps_Distance = ""
        Set routeLI = HTMLdoc.getElementById("altroute_0")
        If Not routeLI Is Nothing Then
            GMaps_Distance = Left(routeLI.innerText, InStr(routeLI.innerText, ", ") - 1)
        End If
    
    End Function
    
    
    Private Function Escape(ByVal paramValue As String) As String
    
        Dim i As Integer, BadChars As String
    
        BadChars = "<>%=&!@#$^()+{[}]|\;:'"",/?"
        For i = 1 To Len(BadChars)
            paramValue = Replace(paramValue, Mid(BadChars, i, 1), "%" & Hex(Asc(Mid(BadChars, i, 1))))
        Next
        paramValue = Replace(paramValue, " ", "+")
        Escape = paramValue
    
    End Function
    If the distance returned is blank, it means that id=altroute_0 doesn't exist in the Google Maps web page when the code is run in USA, so you will have to examine the HTML source to find the correct id and maybe modify that part of the code slightly.

+ 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