Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

546
Visualizações
Google Directions API distance calculation from VBA

I used a tutorial to automatically calculate travel distance using Google Directions API, based on addresses I enter in Excel. I succesfully created an API key and I used a tutorial page that showed two functions in VBA, one for Time and one for Distance:



    'Returns the number of seconds it would take to get from one place to another
Function TRAVELTIME(origin, destination, apikey)
 
    Dim strUrl As String
    strUrl = "https://maps.googleapis.com/maps/api/directions/json?origin=" & origin & "&destination=" & destination & "&key=" & apikey
     
    Set httpReq = CreateObject("MSXML2.XMLHTTP")
    With httpReq
         .Open "GET", strUrl, False
         .Send
    End With
     
    Dim response As String
    response = httpReq.ResponseText
     
    Dim parsed As Dictionary
    Set parsed = JsonConverter.ParseJson(response)
    Dim seconds As Integer
     
    Dim leg As Dictionary
     
    For Each leg In parsed("routes")(1)("legs")
        seconds = seconds + leg("duration")("value")
    Next leg
     
     
     
    TRAVELTIME = seconds
 
End Function
 
 
' Returns the distance
Function TRAVELDISTANCE(origin, destination, apikey)
 
    Dim strUrl As String
    
    strUrl = "https://maps.googleapis.com/maps/api/directions/json?origin=" & origin & "&destination=" & destination & "&key=" & apikey
    
    Set httpReq = CreateObject("MSXML2.XMLHTTP")
    With httpReq
         .Open "GET", strUrl, False
         .Send
    End With
     
    Dim response As String
    response = httpReq.ResponseText
     
    Dim parsed As Dictionary
    Set parsed = JsonConverter.ParseJson(response)
    Dim meters As Integer
     
    Dim leg As Dictionary
        
    For Each leg In parsed("routes")(1)("legs")
        meters = meters + leg("distance")("value")
    Next leg
     
     
     
    TRAVELDISTANCE = meters
 
End Function

When I try to use the distance function, it fails most of the time. I can get it to work if I enter a single dimension of a location, e.g. "Kerkstraat 10", but if I add the city name, e.g. "Kerkstraat 10, Amsterdam", I get a #Value error in Excel.

Whether I use spaces, commas, semicolons, %20 or whatever in VBA doesn't seem to matter and interestingly, the traveltime function works perfectly with a "full" address.

Does anyone know what might be wrong, or if there is a better/different way to get what I need?

Thanks in advance!

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda