Consider a List of Coordinates, which includes the Latitude and Longitude in columns B and C, respectively. We’ll convert these coordinates into human-readable addresses.
Method 1 – Applying VBA Code for a User-Defined Function
Step 1 – Open the Microsoft Visual Basic for Applications IDE
- Go to the Developer tab.
- Select Visual Basic in the Code group of commands.
- The Microsoft Visual Basic for Applications window pops up.
- Go to the Insert tab.
- Select Module from the available options.
- This will add a code module on the right side of the display.
Step 2 – Select the Right Reference
- Go to the Tools tab.
- Click on References… from the options.
- The References-VBAProject dialog box opens.
- In the Available References section, check the box for Microsoft XML, v3.0.
- Click OK.
Step 3 – Build the Script
- Copy the following code and paste it into the module.
Option Explicit
Function ReverseGeocode(lati As Double, longi As Double) As String
On Error GoTo 0
Dim xD As New MSXML2.DOMDocument
Dim URL As String, vbErr As String
xD.async = False
URL = "https://nominatim.openstreetmap.org/reverse?lat=" + CStr(lati) + _
"&lon=" + CStr(longi)
xD.Load ("https://nominatim.openstreetmap.org/reverse?lat=" + CStr(lati) + _
"&lon=" + CStr(longi))
If xD.parseError.ErrorCode <> 0 Then
Application.Caller.Font.ColorIndex = vbErr
ReverseGeocode = xD.parseError.reason
Else
xD.SetProperty "SelectionLanguage", "XPath"
Dim loca As MSXML2.IXMLDOMElement
Set loca = xD.SelectSingleNode(" / reversegeocode / result")
If loca Is Nothing Then
Application.Caller.Font.ColorIndex = vbErr
ReverseGeocode = xD.XML
Else
Application.Caller.Font.ColorIndex = vbOK
ReverseGeocode = loca.Text
End If
End If
Exit Function
0:
Debug.Print Err.Description
End Function
- Save the workbook as an Excel Macro-Enabled Workbook.
Step 4 – Use the User-Defined Function
- Construct a new column named Address under Column D.
- Select cell D5 and insert the following in the cell.
=re
- Select the ReverseGeocode function. Double-click on it or tap the Tab key to put it on the worksheet.
- Provide the arguments of the function and press Enter.
- The first address is converted from the latitude and longitude in cells B5 and C5 and put in cell D5.
- Bring the cursor to the bottom-right corner of cell D5 and it’ll look like a plus (+) sign. That’s the Fill Handle tool.
- Double-click on it to copy the formula up to cell D14.
- All the longitudes and latitudes have been converted to addresses.
- You can compare the result with the following screenshot. It’s a picture of a website from where we get the latitudes and longitudes.
Method 2 – Using an Online Geocoding Tool
Steps:
- Go to Geoapify.com
- We removed the heading from the previous dataset.
- Save the file.
- Scroll down in your browser and you will get a place to input your dataset file.
- Click on the “Browse Files” button.
- In the Open window, find the Excel file.
- Click on Open.
- We can see our file is inserted, and the website is showing a short preview of our dataset.
- Check the boxes for Latitude and Longitude which are our column names.
- Choose lat for Latitude and lon for Longitude.
- We selected English in the Language box.
- Click on the Geocode button.
- Click on Download geocoding results.
- This will save a CSV file on the PC. Open the file to see the result.
Note: We’ve added some formatting to the CSV file for visual beautification. You can go through the article Formatting CSV File in Excel to learn more.
Download the Practice Workbook
<< Go Back to Geocoding in Excel | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!
Is it possible to convert this to post code only? I have 20,000 cases and it lags after I type it in 🙂
Or maybe you know how to do this is in SQL?
Hello GREG,
Thanks for your comment. Yes, you can do it. Here, I’m showing you to do it with Bing Maps API. Because it’s free. To use Google Maps API, you need to register with Card information. So, I have chosen Bing over Google here.
The following VBA macro is the one-stop solution to your problem.
Run this macro and it will ask to input the range containing the latitude and longitude. Make sure to keep a blank column adjacent to the input columns. You’ll get the output there.
Regards,
SHAHRIAR ABRAR RAFID
Team ExcelDemy
What if you only want some of the parameters like Country, State, City, County, Zip Code. How much code would you need to change to get these?
Hi KEVIN,
Thanks for reaching out to us. To get only the parameters you mentioned, adjust the XPath expressions used to select the specific elements from the XML response. Here’s the modified code:
Hope this helped. Let us know your further queries.
Regards,
Aung
Team ExcelDemy
what code would you change to get things other than postal code? Like State, City, or both?
Thanks very much for this
Hello Loni,
You are most welcome.
Regards
ExcelDemy
Hello! Thank you for your work!
Only whole numbers work for me
With GPS coordinates, look picture (line 1
line 2)
What can be the solution? Can you help me?
Picture: https://www.kepfeltoltes.eu/images/2024/02/25/161error_gps.jpg
Hi Zsolt!
You are most welcome. It seems to me that you are trying to write a decimal number using a comma (,) instead of a decimal point(.). If you write the latitudes and longitudes as 42.328674 and -72.664658 instead of 42,328674 and -72,664658 the function should not return any error and you should get your expected result.
Regards,
Nafis
ExcelDemy
Hi,
Is it possible to have a short version of the location, example only city and country?
Hello Mentor,
You will need to modify the VBA function to return only the city and country from the OpenStreetMap response, update the code as follows:
Adjust the URL to request JSON data instead of XML by adding &format=json. Parse the JSON response to extract only the “city” and “country” fields.
Here’s the updated VBA code:
Important Notes:
Ensure that you have a JSON parser installed in VBA (e.g., VBA-JSON).
This code returns only the city and country as requested.
Regards
ExcelDemy