Pinterest Pixel

How to Calculate Distance Between Two Cities in Excel – Step by Step Guide

John Michaloudis
When working with location-based data, I often need to calculate the distance between cities in Excel.
While Excel doesn't have a built-in function for this, there are multiple methods to get the job done as I will show below.

When working with location-based data, I often need to calculate the distance between cities in Excel. While Excel doesn’t have a built-in function for this, there are multiple methods to get the job done as I will show below.

Key Takeaways:

  • Excel’s Geography Data Type helps retrieve latitude and longitude for cities.
  • The Haversine formula calculates the shortest distance between two points using coordinates.
  • Google Maps links can be generated in Excel to find distances visually.
  • VBA can automate distance retrieval using the Google Maps API.

 

Unlocking Excel for Distance Calculations

Preparing the Dataset

Before calculating the distance, I need to ensure that my dataset is well-structured. The key elements of my dataset are:

  • City Names: Names of the cities for which I want to find distances.
  • Latitude and Longitude: Geographic coordinates that Excel can fetch using the Geography Data Type.
  • Distance Calculation Methods: Various approaches to computing the distance.

To start, I enter the names of cities in Column A:

Distance Between Two Cities in Excel

Extracting Latitude and Longitude Using the Geography Data Type

Excel has a built-in Geography Data Type that allows me to retrieve location-specific details, including latitude and longitude.

STEP 1: Highlight the city names in the range A2:A5.

Distance Between Two Cities in Excel

STEP 2: Go to the Data tab. Click on Geography in the Data Types group.

Distance Between Two Cities in Excel

Excel converts the city names into geographical entities (indicated by a small map icon next to the city name).

Distance Between Two Cities in Excel

STEP 3: Click on the small button and select Latitude as the field.

Distance Between Two Cities in Excel

STEP 4: Next, select Longitude as a field.

Distance Between Two Cities in Excel

STEP 5: Drag the formula down.

Distance Between Two Cities in Excel

 

Methods to Calculate Distance Between Two Cities

Method 1: Using the Haversine Formula

The Haversine formula calculates the shortest distance between two points on a sphere using latitude and longitude.  It looks complicated initially but once you have the formula setup it is very easy to use.

=6371 * ACOS(COS(RADIANS(90 – A2)) * COS(RADIANS(90 – A3)) + SIN(RADIANS(90 – A2)) * SIN(RADIANS(90 – A3)) * COS(RADIANS(B2 – B3)))

  • 6371 is the Earth’s radius in kilometers.
  • ACOS calculates the arc cosine.
  • RADIANS converts degrees to radians.

Distance Between Two Cities in Excel

This will provide the distance in kilometers.

Method 2: Using a Google Maps Link

Use the CONCATENATE function to create a clickable Google Maps link, this gives a visual view of your route when you click on it.

=HYPERLINK(“https://www.google.com/maps/dir/” & A2 & “/” & A3, “Find Distance”)

Distance Between Two Cities in Excel

Click the Link to open Google Maps with the route displayed.

Distance Between Two Cities in Excel

Method 3: Using VBA

VBA (Visual Basic for Applications) can fetch the distance from Google Maps API. Enable the Developer Tab in Excel if it’s not already enabled.

STEP 1: Open VBA Editor (ALT + F11) and insert a new module.

Distance Between Two Cities in Excel

STEP 2: Paste the following VBA code:

Function GetDistance(origin As String, destination As String) As String
Dim http As Object
Dim JSON As Object
Dim URL As String
URL = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" & origin & "&destinations=" & destination & "&key=YOUR_API_KEY"
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", URL, False
http.Send
Set JSON = JsonConverter.ParseJson(http.responseText)
GetDistance = JSON("rows")(1)("elements")(1)("distance")("text")
End Function

Distance Between Two Cities in Excel

STEP 3: Replace ‘YOUR_API_KEY’ with your Google Maps API key.

STEP 4: Call the Function in Excel:

=GetDistance(“New York, USA”, “Los Angeles, USA”)

Press Enter to get the distance in miles or kilometers.

 

Frequently Asked Questions

How can I get the latitude and longitude using Excel?

Highlight city names, go to the Data tab, and select the Geography Data Type. Then, select Latitude and Longitude as fields to retrieve the geographic coordinates.

How can I use Google Maps in Excel?

You can use Google Maps in Excel by generating clickable links with the HYPERLINK function. This lets you create direct links to view distances between locations on Google Maps.

How do I automate distance calculations?

To automate distance calculations, you can use VBA (Visual Basic for Applications) with the Google Maps API to fetch distance data directly from the Maps service based on city names or addresses.

If you like this Excel tip, please share it


Founder & Chief Inspirational Officer

at

John Michaloudis is a former accountant and finance analyst at General Electric, a Microsoft MVP since 2020, an Amazon #1 bestselling author of 4 Microsoft Excel books and teacher of Microsoft Excel & Office over at his flagship MyExcelOnline Academy Online Course.

See also  Convert Date to Text in Seconds using Excel's Top 3 Methods

Star 30 Days - Full Access Star

One Dollar Trial

$1 Trial for 30 days!

Access for $1

Cancel Anytime

One Dollar Trial
  • Get FULL ACCESS to all our Excel & Office courses, bonuses, and support for just USD $1 today! Enjoy 30 days of learning and expert help.
  • You can CANCEL ANYTIME — no strings attached! Even if it’s on day 29, you won’t be charged again.
  • You'll get to keep all our downloadable Excel E-Books, Workbooks, Templates, and Cheat Sheets - yours to enjoy FOREVER!
  • Practice Workbooks
  • Certificates of Completion
  • 5 Amazing Bonuses
Satisfaction Guaranteed
Accepted paymend methods
Secure checkout

Get Video Training

Advance your Microsoft Excel & Office Skills with the MyExcelOnline Academy!

Dramatically Reduce Repetition, Stress, and Overtime!
Exponentially Increase Your Chances of a Promotion, Pay Raise or New Job!

Learn in as little as 5 minutes a day or on your schedule.

Learn More!

Share to...