In this article, we will create an Excel function to calculate the distance between two addresses using the Google Maps directions API. This will allow you to get the travel time between the two locations. The format of the function will be as follows: =TRAVELTIME(origin, destination, api_key)
, =TRAVELDISTANCE(origin, destination, apikey)
. The origin and destination will be strings, and can be either an exact address or the name of a place. In order to use the function, an API key is required. The “Getting Started” page can help you with this: http://bit.ly/googlemapsgettingstarted. Create a new project and make sure the Directions API is added.
Activate Microsoft 365, 2019, 2016, and 2013. When you start an Office app that's not activated, you'll be prompted to sign in to Office. Be sure to sign in using the Microsoft account, or work or school account, that you used to buy or subscribe to Microsoft 365. Note: If Office came pre-installed on your new device, see Activate Office that's. I would like to stream data from a websocket server using VBA in an Excel workbook. This is the actual data stream I would like to receive and process in VBA: Would you recommend using WinHttpRequest (WinHTTP Services) or any other service to connect and receive the websocket data?
Step 1: Create a new macro file and add VBA-JSON
Because the Google Maps Directions API is a JSON API, we will use VBA-JSON to make it easy to use the results from the web request. You can download the latest version from here: https://github.com/VBA-tools/VBA-JSON/releases. Download and extract the zip file. Then, open your macro file. Open the Visual Basic Editor (Alt + F11).
In order to import the VBA-JSON file, go to File > Import File… (Ctrl + M). Select JsonConverter.bas
. A JsonConverter module will appear in the sidebar.
Next, make sure the appropriate references are enabled. Go to Tools > References… In addition to the references already selected, check off “Microsoft Scripting Runtime” (for Dictionary support needed by VBA-JSON) and “Microsoft WinHTTP Services, version 5.1” (to make the HTTP request to the API). If you require support for Excel for Mac, you will need to install VBA-Dictionary from the author of VBA-JSON. More details can be found at the bottom of the project homepage: https://github.com/VBA-tools/VBA-JSON.
Step 2: Create the functions
With the references configured, we can now write the code for the function. The code is relatively simple. It is simply takes the three parameters and formats them into a web request. The response of the web request is then parsed by VBA-JSON and the relevant variable returned. Note that the request may return multiple routes, but then function simply returns the time of the first route. The default mode is driving, but refer to the Directions API documentation for information on other modes and adjust the strURL variable accordingly.
To insert the code, create a new module with Insert > Module. Then paste the following code:
Save the file. You should now be able to use the functions from within Excel. Place your API key in cell A1, then try the following: =TRAVELTIME('24 Sussex Drive Ottawa ON', 'Parliament Hill', A1)
. This returns a travel time of about 435 seconds. If you would like this to be displayed in minutes and seconds, try this function: =FLOOR.MATH(A8/60)&' minutes '&MOD(A8, 60)&' seconds'
where A8 is the cell with the travel time in seconds. This prints a helpful “7 minutes 15 seconds” for the 24 Sussex example. We can also find the distance. Try the following: =TRAVELDISTANCE('24 Sussex Drive Ottawa ON', 'Parliament Hill', A1)
. It returns a distance of 2667 meters. Convert to kilometers with this: =ROUND(A9/1000, 1)&' km'
.
Note: The Google Maps Directions API always returns distances in meters. Convert to KM or Miles as you wish. This can be done in Excel or by modifying the functions in VBA.
That’s it! You should now have a working travel time function. All you need now is a list of addresses to use it with. If you would like to pull a list from the web or local JSON file, check out Import JSON Data in Excel 2016 or 2019 or Office 365 using a Get & Transform Query.
-->Users of Microsoft Windows HTTP Services (WinHTTP) should use the latest version of the technology, version 5.1. Version 5.0 is no longer supported.
Version 5.1
WinHTTP 5.1 offers improvements over version 5.0; for more information about new features, see What's New in WinHTTP 5.1.
With version 5.1, WinHTTP is an operating-system component of the following operating systems:
- Windows 2000, Service Pack 3 and later (except Datacenter Server)
- Windows XP with Service Pack 1 (SP1) and later
- Windows Server 2003 with Service Pack 1 (SP1) and later
Excel Install Microsoft Winhttp Services
For Windows Server 2003 with SP1, WinHTTP is a system side-by-side assembly. For more information, see Side-by-side Assemblies.
Redistribution of WinHTTP 5.1 outside of these operating systems is not supported.
Version 5.0
The WinHTTP 5.0 redistributable, 'winhttp5.dll', runs on Windows 2000 or later, or Windows NT 4.0 with Microsoft Internet Explorer 5.01 or later.
Excel Install Microsoft Winhttp Services Version
Important
The WinHTTP 5.0 download is no longer available. As of October 1, 2004, Microsoft has removed the WinHTTP 5.0 SDK download from MSDN and has terminated product support for version 5.0.