How to Convert ZIP Code to Latitude and Longitude in Excel

A ZIP (Zone Improvement Program) Code is a term used in the United States postal code system to define a specific location. Latitudes are horizontal lines that indicate how far north or south of the equator a location is. Longitudes are vertical lines that measure how far to the east or west of Greenwich, England’s meridian, a location is. Together, latitude and longitude allow cartographers, geographers, and others to pinpoint locations on Earth. Negative Latitude and Longitude values reflect the southern and western hemispheres respectively, while positive Latitudes and Longitudes indicate the northern and eastern hemispheres respectively.

In this article, we will demonstrate 2 methods to convert a Zip code to Latitude and Longitude coordinates in Excel using the below data set.

convert zip code to latitude longitude excel

We used Microsoft Excel 365 here. The first Method requires Excel 365, but the second may work in earlier versions of Excel too.


Method 1 – Convert Zip Code to Latitude and Longitude Using Geography Data Type

Geography is a new data type accessible to Microsoft 365 users, which enables us to Convert ZIP Codes to Latitude and Longitude as well as access information such as population, time zone, region leaders, gas costs, and language. To input Geographic Data into Excel, the content must be converted to the Geography data type first.

STEPS:

  • Select the range from B5 to B10.
  • Navigate to the Data ribbon and then Geography.

Convert Zip Code to Latitude and Longitude Through Geography Data Type

  • Select cell C5.
  • Enter the formula below:
=B5.City.Latitude
  • Press the Tab key to view the result.

  • Use the Fill Handle tool to drag the formula down to cell C10.

The Latitude coordinates are returned in column C.

  • Select cell D5.
  • Enter the formula below:
=B5.City.Longitude
  • Press Enter.

  • Again, use the Fill Handle tool to drag the formula down to cell D10.

The Longitude coordinates are filled in column D.

output of zip code in latitude longitude


Method 2 – Apply VBA Code to Convert a Zip Code to Latitude and Longitude

STEPS:

  • Select the active sheet of the workbook.
  • Go to the Developer tab.
  • Click on Visual Basic.

Apply Excel VBA to Convert the Zip Code to Latitude Longitude

  • Select Insert and then Module to open the module box.
  • Click the Tools menu item.
  • Select References from the drop-down list.

  • Check Microsoft XML, v3.0.
  • Click OK.

  • Enter the following code in the module box and click the Save icon:
Function findLatitude(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
        findLatitude = 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
            findLatitude = loc.getAttribute("lat")
        End If
    End If
End Function
Function findLongitude(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
        findLongitude = 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
            findLongitude = loc.getAttribute("lon")
        End If
    End If
End Function

Note
Use a valid API endpoint. For example, you may find that “https://nominatim.openstreetmap.org/search?format=xml&q=” is invalid. Sometimes, APIs change their URLs or endpoints. The API Key for this particular service, which provides geocoordinate information, becomes invalid after three months or 10K transactions are reached.
  • In cell C5 enter the formula below:
=@findLatitude(B5)
  • Press Enter to view the result.

  • Use the Fill Handle tool to copy the formula down to cell C10 below.

Latitude values are filled in column C.

  • As before, select cell D5 and enter the formula below:
=@findLongitude(B5)
  • Press Tab to display the result.

  • Use the Fill Handle to copy the formula down to cell D10.

Longitude results are returned in column D.

output of Zip Code in Latitude Longitude by VBA


Download Practice Workbook


<< Go Back to Geocoding in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Lutfor Rahman Shimanto
Lutfor Rahman Shimanto

Lutfor Rahman Shimanto, BSc, Information Technology, Jahangirnagar University, Bangladesh, has worked with the ExcelDemy project for over a year. He has written 50+ articles and provided solutions of 100+ comments for ExcelDemy. Currently, he works as an Excel & VBA Developer and provides support and solutions in the ExcelDemy Forum. He has solved 100+ ExcelDemy Forum problems. His work and learning interests are in developing various Excel & VBA and Desktop applications. Outside of work, he enjoys Chess... Read Full Bio

2 Comments
  1. Can you please explain why the latitude/longitude values from Option 2 are all slightly different than the values from Option 1? Is Option 2 more accurate/precise than 1?

    • Reply Lutfor Rahman Shimanto
      Lutfor Rahman Shimanto Oct 15, 2023 at 6:18 PM

      Hello HEATHER

      Thanks for reaching out and posting your comment. You are right about the results from using Geography Datatypes and Excel VBA user-defined functions being slightly different. However, a valid API endpoint in VBA code will give you accurate results like the Geography datatypes.

      If you are a Microsoft 365 user, I recommend using the first method, where you can use Geography datatype. I have presented the Excel VBA to other Excel users. However, I must admit that using the Geography is more accurate than the Excel VBA user-defined functions.

      Regards
      Lutfor Rahman Shimanto

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo