Zip Code reference and ZOHO sheets

HomePro

Board Regular
Joined
Aug 3, 2021
Messages
157
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
  10. Prefer Not To Say
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
So i was able to build the Zip Code look up from a spreadsheet I found online. It works fine in Excel. I uploaded it into ZOHo Sheets [ its what our office uses] and although they say it supports VBA it will not run.

Here is the code:
VBA Code:
Sub putdistanceonsheet()
Dim wks1 As Worksheet
Dim blnDone As Boolean, strBaseAddr As String, strGuestAddr As String
Dim strDist As String, p As Integer
Dim strAPIKey As String, strTravelMode As String, strRegion As String
Set wks1 = Application.ActiveSheet
blnDone = False
strAPIKey = "CnjvtWyrEGlWezTAI0zas2aNeCpJWsEVVOnulmXFwcV6E9oHrjCoE7XIj299YBCn "
'strTravelMode = Range("C3")
'strRegion = Range("C4")
wks1.Activate
wks1.Range("C5").Select
strBaseAddr = Range("A1")
strBaseAddr = WorksheetFunction.EncodeURL(strBaseAddr)
While Not blnDone
'select up to 20 destination addresses for 1 query
strDestAddr = ""
For r = 0 To 40
    If Not IsEmpty(ActiveCell.Offset(r, 0)) Then
        If Len(strDestAddr) > 0 Then
            strDestAddr = strDestAddr & "|"
        End If
        strDestAddr = strDestAddr & ActiveCell.Offset(r, 0)
    Else
        Exit For
    End If
Next
strDestAddr = WorksheetFunction.EncodeURL(strDestAddr)
aryDistData = gDist(strBaseAddr, strDestAddr, strAPIKey, strTravelMode, strRegion)
If aryDistData(0, 0) <> "NO DATA" Then
    For d = 0 To UBound(aryDistData)
        ActiveCell.Offset(d, 2) = aryDistData(d, 0)
        ActiveCell.Offset(d, 1) = aryDistData(d, 1)
    Next
    ActiveCell.Offset(r, 0).Select 'move down to the next row
End If
If IsEmpty(ActiveCell) Then
    blnDone = True
End If
Wend
'sORT AND PREP SHEET
    Range("A5:E40").Select
    Range("E5").Activate
    ActiveWindow.SmallScroll Down:=-160
    ActiveWorkbook.Worksheets("Inspector Travel Times").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Inspector Travel Times").Sort.SortFields.Add2 Key _
        :=Range("E5:E20"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Inspector Travel Times").Sort
        .SetRange Range("A5:E20")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1:B2").Select
End Sub

Function gDist(strOrig, strDest, strAPI, strMode, strReg)

Dim strURL As String
Dim objHttp As MSXML2.XMLHTTP60
Dim objDom As DOMDocument60
Dim aryDest() As String
' in case region code is not included from cheet
If Len(strReg) > 0 Then
    strRegURL = "&region=" & strReg
End If
        strURL = "[URL]https://maps.googleapis.com/maps/api/distancematrix/xml?units=imperial[/URL]" & _
        "&origins=" & strOrig & _
        "&destinations=" & strDest & _
        "&traffic_mode1=optimistic" & _
        "&mode=" & strMode & _
        strRegURL & _
        "&key=" & strAPI
Set objHttp = New MSXML2.XMLHTTP60
With objHttp
    .Open "GET", strURL, False
    .setRequestHeader "Content-Type", "application/x-www-form-URLEncoded"
    .Send
End With
Set objDom = New DOMDocument60
objDom.LoadXML (objHttp.responseText)
'objDom.LoadXML objXHTTP.responseText
Dim strStatus As String
strStatus = objDom.SelectSingleNode("//status").Text
If strStatus = "OK" Then 'we have data to parse
    numrows = objDom.SelectNodes("//row/element").Length
    ReDim aryDest(numrows - 1, 1)
    'get the rows
    For x = 1 To numrows
        Dim datanode As MSXML2.IXMLDOMNode
        Set datanode = objDom.SelectNodes("//row/element")(x - 1)
        If datanode.SelectNodes("status")(0).Text = "OK" Then
            strDur = datanode.ChildNodes(1).ChildNodes(0).Text
            strDur = Str(Round(Val(strDur) / 60, 1))  'convert seconds to minutes
            strDist = datanode.ChildNodes(2).ChildNodes(0).Text
            strDist = Round(Val(strDist) / 1609.344, 3) 'convert from meters to miles
            aryDest(x - 1, 0) = strDur  'durations in seconds, converted to minutes
            aryDest(x - 1, 1) = strDist  'distance in meters converted to miles
        Else
            aryDest(x - 1, 0) = datanode.SelectNodes("status")(0).Text
            aryDest(x - 1, 1) = datanode.SelectNodes("status")(0).Text
        End If
    Next
Else
    ReDim aryDest(0, 0)
    aryDest(0, 0) = "NO DATA"
End If
Set objDom = Nothing
Set objHttp = Nothing
gDist = aryDest
End Function








Inspector Travel Times.xlsm
ABCDE
132081
2
3To LocationMile to DestinationTravel Time (minutes)
4
5AustinBarbour Jr.3208100
6GaryBurbage322561926
7LarryNewland322242126
8JoeGalison322591627
9AnthonyMasciello322591627
10BryanVanArsdale322572128
11ShannonTrahan322162330
12JeffSmith322072632
13CristinaSantiago322112936
14FordMullen322053338
15MarvinPittman322093438
16AndyToole322333142
17RyanCoker320115359
18ThomasDeGrove320345869
19BradRushing320345869
Inspector Travel Times
 
Last edited by a moderator:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Appreciated. That will help
 
Upvote 0
You are welcome.
 
Upvote 0
Appreciated. That will help
Does not seem that that board wil be very helpful but I will wait and hope. In the meantime does anyone with more expirence than me think they can consolidate the script into one macro as opposed to three section? perhaps then it will work for me in Zoho.
 
Upvote 0

Forum statistics

Threads
1,215,113
Messages
6,123,165
Members
449,099
Latest member
afishi0nado

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