When analyzing real estate investments in Excel, it’s often necessary to include the latitude and longitude coordinates of the properties we’re modeling. To do this, most of us open up Google Maps, or some other mapping tool, and find the coordinates that way. Others use a latitude/longitude lookup tool such as latlong.net to find what they need.
However, these tools require leaving Excel, manually grabbing the coordinates, and then copying them back into Excel. While not overly time consuming, there is a more effective and accurate way to quickly grab a property’s coordinates without leaving Excel. In this post, I share some VBA code that you can add to your Excel file (together with a Google Geocoding API key) to automate the process of auto-populating latitude and longitude in Excel.
Google Geocoding in Excel
Google has developed a service for converting addresses (like “555 Main St., Anywhere USA, CA”) into geographic coordinates (like latitude 36.411022 and longitude -120255489). The service, made possible by the Google Maps Geocoding API, is commonly used online to quickly convert physical addresses to coordinates.
What is less common, but just as easily done, is to use the geocoding API with a custom Excel function or via an Excel Add-In to convert a physical address to latitude and longitude coordinates instantly and without having to leave Excel. In this tutorial, I show you how to use VBA code to create a custom Excel function. If you’d rather use an Excel add-in, checkout our A.CRE Geocoding Excel Add-in.
Note: This function requires you to insert a Google Geocoding API key (which you can get for free – instructions below). The API sets a limit on the number of requests you can make each second (50). When you exceed the limit, you get a message that reads something like: “Requestor has exceeded the server limit.” Google offers the ability to increase the limit via a premium plan. Google also now requires you to have a credit card on file and charges you for API use above a certain threshold. Learn more about Google’s Usage Limits Here.
Manually Add a Google Geocoding Custom Function via VBA
The first option for incorporating Google Geocoding into Excel, is to write a custom function that will take an address and instantly convert it to latitude and/or longitude coordinates using Google’s geocoding service.
Now if you’re unfamiliar with the concept of custom functions in Excel, a custom function acts like any other function in Excel (e.g. SUM, EDATE, VLOOKUP) only that it is specific to the workbook you create it in, and requires you to save the workbook as a macro-enabled file. This may be exactly what you want since, if you share the workbook, other users of the workbook will have access to the custom function.
However, if you’d like this function to be available every time you open up Excel, you might consider installing the A.CRE Geocoding Excel Add-in.
Adding the Google Geocoding custom function only takes a few minutes and the process is quite painless. I learned how to do this after reading a post on the subject, written by Christos Samaras, on his engineering blog (of all places!). I’ve recorded a video (see below) that walks you through the simple process of creating the function using Mr. Samaras’ VBA code. The video also explains how to get and insert your Google Geocoding API key.
To get the VBA Code used in this tutorial: Click Here
Once the custom function is in your model, using the function is simple:
- In the cell of your choosing (e.g. cell B2), write a physical address in this order: STREET ADDRESS, CITY, STATE, ZIP CODE (example: 555 Main St, Anywhere USA, CA, 90229).
- To get the latitude of the address in cell B2, use the formula = GetLatitude(B2)
- To get the longitude of the address in cell B2, use the formula = GetLongitude(B2)
- To get both the latitude and longitude of the address in cell B2, use the formula = GetCoordinates(B2)
Here are the most commons issues that arise when manually using the Geocoding custom function:
- Failure to Enable XML v3.0 library. In order to use this function you must enable the XML, v3.0 library from VBA editor. To do this, ‘Go to Tools -> References -> check the Microsoft XML, v3.0.
- You haven’t inserted a proper API Key into the code. As of 2018, the code requires you to insert your own Google Maps Geocoding API key. And as of 2019, it’s necessary to attach billing information to your Google account and link that to your Google project. To get a free API key from Google, use this quick start guide provided by Google.
- Getting a #VALUE, #NAME, or ‘Server denied the request’ errors. There are a handful of reasons for these errors but generally these errors are because either the function wasn’t setup correctly or the Google API key wasn’t setup correctly. My best advice if you’re struggling with one of these error is to:
- Follow the instructions to the T. I know that’s easier said than done, but if you’re not saving the file as a Macro-Enabled file, not turning on Microsoft XML 3.0, entering the API key in the wrong place, inadvertently deleting something from the VBA code, or not entering the address correctly, etc, than you will get an error.
- Make sure you’re running the most recent version of Excel. This code may work with older version of Excel, but I have not tested it on versions earlier than 2016.
- Make sure you have billing details on file with Google in order for your API key to work. While Google gives you a certain number of API requests for free, Google requires users have a credit card on file to avoid abuse of the API.
- Also here’s a quick tip. If you use the GETCOORDINATES() function, the VBA code has a few built-in custom error messages that will help you troubleshoot what you’re doing wrong. For instance, if you haven’t setup your API key correctly, the cell will output: “Invalid API Key”.
Alternative: Use the A.CRE Geocoding Excel Add-in
While writing a custom function is a viable solution for converting addresses to latitude and longitude coordinates, it has two disadvantages. First, it requires going into the VBA editor and manipulating code. That can be an intimidating task for some. The second issue with writing a custom function, is that it only exists in that one Excel workbook, which means if you want the function available beyond that Workbook you’ll need to create a custom Excel Add-in.
Unfortunately, creating a custom Excel add-in is not as simple as writing a custom function. So we’ve gone ahead and created a downloaded custom Excel add-in for this particular task. That add-in is called the A.CRE Geocoding Excel Add-in. You can learn more about the add-in and how to install and use it by clicking here.