Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

552
Views
Cálculo de distancia de Google Directions API desde VBA

Utilicé un tutorial para calcular automáticamente la distancia de viaje con la API de direcciones de Google, según las direcciones que ingresé en Excel. Creé con éxito una clave API y usé una página de tutorial que mostraba dos funciones en VBA, una para el tiempo y otra para la distancia:

 '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

Cuando trato de usar la función de distancia, falla la mayor parte del tiempo. Puedo hacer que funcione si introduzco una sola dimensión de una ubicación, por ejemplo, "Kerkstraat 10", pero si agrego el nombre de la ciudad, por ejemplo, "Kerkstraat 10, Ámsterdam", aparece el error #Value en Excel.

Ya sea que use espacios, comas, puntos y comas, %20 o lo que sea en VBA, no parece importar y, curiosamente, la función de tiempo de viaje funciona perfectamente con una dirección "completa".

¿Alguien sabe qué podría estar mal, o si hay una forma mejor/diferente de obtener lo que necesito?

¡Gracias por adelantado!

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!