Woody Pecker
New Member
- Joined
- Apr 11, 2008
- Messages
- 35
- Office Version
- 365
- Platform
- Windows
- MacOS
I'm very new to VBA & to be honest I'm not 100% what I'm doing.
I'm putting together a quick Excel sheet which will extract data from Google Maps for Drive Time in hours & minutes & Distance in miles.
Here's the code I currently have:-
I have obtained my companies API Key, but I don't want to post on here for IT security purposes.
I have also been given another piece of software JsonConverter library, but when added & the VBA ran I get the following error message, so can anyone assist?
To use the JsonConverter library in your VBA code, you need to import the JsonConverter module into your Excel workbook. Here's how to do that:
"Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As Dictionary"
I'm putting together a quick Excel sheet which will extract data from Google Maps for Drive Time in hours & minutes & Distance in miles.
Here's the code I currently have:-
VBA Code:
Private Sub GetDrivingTime()
Dim origin As String
Dim destination As String
Dim apiKey As String
Dim url As String
Dim httpRequest As New XMLHTTP60
Dim jsonResponse As String
Dim jsonObject As Object
Dim distance As String
Dim duration As String
' Replace with your own Google Maps API key
apiKey = "YOUR_API_KEY_HERE"
origin = "1600 Amphitheatre Parkway, Mountain View, CA"
destination = "20 W 34th St, New York, NY 10001"
url = "[URL]https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=[/URL]" & _
origin & "&destinations=" & destination & "&key=" & apiKey
With httpRequest
.Open "GET", url, False
.Send
jsonResponse = .ResponseText
End With
' Parse the JSON response using the JsonConverter library
Set jsonObject = JsonConverter.ParseJson(jsonResponse)
distance = jsonObject("rows")(1)("elements")(1)("distance")("text")
duration = jsonObject("rows")(1)("elements")(1)("duration")("text")
MsgBox "The driving distance is: " & distance & vbNewLine & "The driving time is: " & duration
End Sub
I have obtained my companies API Key, but I don't want to post on here for IT security purposes.
I have also been given another piece of software JsonConverter library, but when added & the VBA ran I get the following error message, so can anyone assist?
To use the JsonConverter library in your VBA code, you need to import the JsonConverter module into your Excel workbook. Here's how to do that:
- Download the JsonConverter library from the GitHub repository: GitHub - VBA-tools/VBA-JSON: JSON conversion and parsing for VBA
- Extract the ZIP file to a location on your computer.
- In Excel, go to the "Developer" tab and click on the "Visual Basic" button to open the VBA editor.
- In the VBA editor, go to the "File" menu and select "Import File".
- Navigate to the location where you extracted the JsonConverter library and select the "JsonConverter.bas" file.
- The JsonConverter module should now be imported into your workbook and accessible in your VBA code.
"Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As Dictionary"
Last edited by a moderator: