How to Use Geocoding in Excel? – 2 Examples

Geocoding is the process of converting addresses or place names into geographic coordinates. Geographic coordinates are latitude and longitude.

 

overview of geocoding in excel


Download Practice Workbook


What Do You Mean by Geocoding in Excel?

Geocoding in Excel is the process of converting an address into geographic coordinates.

The Latitude is the distance of a location from the equator. It determines the position of a location in the north or south direction.

The Longitude is how far east or west a location is from the Prime Meridian. It indicates its position in the east-west direction.


 

Example 1 -How to Geocode in Excel with Geography Data Type

  • There are city names in column B5:B14.
  • Select the column and go to the Data tab > Data types > Geography.

select geography from data types under Data tab

  • Enter the following formula in C5 to get the latitude of the city.
=B5.Latitude

insertion of formula to get latitude

  • To get the longitude, enter the following formula in D5.
=B5.Longitude

inserting formula to get longitude

  • Drag down the Fill Handle to see the result in the rest of the cells.

Use fill handle to get latitude and longitude for all cities

Note: Geography data type was introduced in Excel 365. If you are using an older conversion of Excel, you may not find this feature.

Read More: Convert Degrees Decimal Minutes to Decimal Degrees


Example 2 – How to Use VBA for Geocoding in Excel

dataset for geocoding using VBA

  • Go to the Developer tab > Visual Basic.

go to visual basic in developer tab

  • Choose Insert > Module.

insert new module from insert menu

  • Select a reference in the tools panel. Go to Tools > References.

go to references from tools

  • The VBAProject dialog box opens > Select Microsoft XML, v3.0, and click OK.

select appropriate reference

  • Enter the following code in the module :
Function Co_Ordinates(address As String) As String
    Application.Caller.Font.ColorIndex = xlNone
    Dim xDoc As New MSXML2.DOMDocument
    xDoc.async = False
    xDoc.Load ("https://nominatim.openstreetmap.org/search?format=xml&q=" + WorksheetFunction.EncodeURL(address))
    If xDoc.parseError.ErrorCode <> 0 Then
        Application.Caller.Font.ColorIndex = vbErr
        Co_Ordinates = xDoc.parseError.reason
    Else
        xDoc.SetProperty "SelectionLanguage", "XPath"
        Dim loc As MSXML2.IXMLDOMElement
        Set loc = xDoc.SelectSingleNode("/searchresults/place")
        If loc Is Nothing Then
            Application.Caller.Font.ColorIndex = vbErr
            NominatimGeocode = xDoc.XML
        Else
            Application.Caller.Font.ColorIndex = vbOK
            Co_Ordinates = loc.getAttribute("lat") & "," & loc.getAttribute("lon")
        End If
    End If
End Function

VBA code for geocoding

  • Save the code and close the module.
  • Enter the following formula in C5. The formula will return the latitude and longitude of the address in B5. Copy the formula using the flash fill.
=Co_Ordinates(B5)

formula for geocoding with VBA

Read More: Perform Reverse Geocoding


 Things to Remember

  • Save your Excel file as XLSM if there are any VBA codes.

Frequently Asked Questions

1. Are there any functions available in Excel that geocode automatically?

Ans: There isn’t any formula available for geocoding purposes.

2. How do I geocode from an address in Excel?

Ans: If you have a city name, you can use Excel’s Geocode feature to find latitude and longitude. If you have a street address, it is better to use VBA.

3. How to add latitude and longitude in Excel?

Ans: You can manually add the latitude and longitude of different places in Excel or use the Geocode feature to extract latitude and longitude from a city address.


Geocoding in Excel: Knowledge Hub


<< Go Back to Learn Excel

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Maruf Niaz
Maruf Niaz

Md. Maruf Niaz, BSc, Textile Engineering, Bangladesh University of Textiles, has worked with the ExcelDemy project for 11 months. He works as an Excel and VBA Content Developer who provides easy solutions to Excel-related problems and regularly writes fantastic content articles. He has published almost 20 articles in ExcelDemy. As an Excel & VBA Content Developer for ExcelDemy, he offers solutions to complex issues. He demonstrates an enthusiastic mindset and adeptness in managing critical situations with finesse, showcasing... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo