How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (2024)

Here is an overview of how the distance between two addresses can be found in Excel.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (1)

How to Calculate the Distance Between Two Addresses in Excel: 3 Ways

In our dataset, we have the Latitude and Longitude for Ohio and Alaska. We will measure the distance between them.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (2)

Method 1 – Using the Haversine Formula to Calculate the Distance Between Two Addresses

Although the Haversine formula will give an approximate result, it’s a pretty good approximation. The formula is given below.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (3)

S = Distance between two addresses

r = Radius of Earth

φ1= Latitude of the first place

φ2= Latitude of the second place

1 = Longitude of the first place

2 = Latitude of the second place

Steps:

  • Insert the following formula in cell C8.

=2*6400*ASIN(SQRT((SIN(RADIANS((C6-C5)/2)))^2+COS(RADIANS(C5))*COS(RADIANS(C6))*(SIN(RADIANS((D6-D5)/2)))^2))

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (4)

We put the radius of the earth in kilometers as 6,400 km so the formula returns kilometers.

φ1 = C5

φ2 = C6

1 = D5

2 = D6

  • Press Enter.
  • If you want to measure the distance in miles, use the following formula in cell C8instead:

=2*3959*ASIN(SQRT((SIN(RADIANS((C6-C5)/2)))^2+COS(RADIANS(C5))*COS(RADIANS(C6))*(SIN(RADIANS((D6-D5)/2)))^2))

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (5)

  • There is also Cosine version of this formula which is simpler to use:

=ACOS(SIN(RADIANS(C5)) *SIN(RADIANS(C6)) +COS(RADIANS(C5)) *COS(RADIANS(C6)) *COS(RADIANS(D5-D6))) * EARTH_RADIUS

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (6)

Note:The formula uses the assumption of the Earth having a spherical shape, while it’s actually an ellipsoid. The Earth is roughly 22 kilometers “shorter” pole-to-pole than it is “wide,” which results in minor deviations.

Method 2 – Using VBA to Calculate the Distance Between Two Addresses in Excel

The VBA code uses public APIs to source information online and return the result in the table.

Steps:

  • Go to Developer and select Visual Basic.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (7)

  • Select Insert and Module to open the VBA Module.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (8)

  • Insert the following code in the VBA Module. We are creating an User Defined Function to calculate the distance in miles.
Option ExplicitPublic Function DistanceInMiles(First_Location As String, _Final_Location As String, Target_Value As String)Dim Initial_Point As String, Ending_Point As String, _Distance_Unit As String, Setup_HTTP As Object, Output_Url As StringInitial_Point = "https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?origins="Ending_Point = "&destinations="Distance_Unit = "&travelMode=driving&o=xml&key=" & Target_Value & "&distanceUnit=mi"Set Setup_HTTP = CreateObject("MSXML2.ServerXMLHTTP")Output_Url = Initial_Point & First_Location & Ending_Point & Final_Location & Distance_UnitSetup_HTTP.Open "GET", Output_Url, FalseSetup_HTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"Setup_HTTP.Send ("")DistanceInMiles = Round(Round(WorksheetFunction.FilterXML(Setup_HTTP.ResponseText, _"//TravelDistance"), 3), 0)End Function

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (9)

Code Explanation

  • We named our Function as DistanceInMiles. We also inserted 3 parameters: First_Location As String, Final_Location As String and Target_Value As String.
  • We declared Initial_Point, Ending_Point, Distance_Unit and Outout_Url as String; Setup_HTTP as Object.
  • We set Initial_Point as the starting of the Url link, Ending_Point as Destination and Distance_Unit to Miles.
  • We set the necessary parameters to create a relation between our VBA code and the API
  • We established our User Defined Function.
  • We made another User Defined Function to find the distance in kilometersby changing the distanceUnit to kilometers.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (10)

  • You can see the API key in cell C8.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (11)

  • Use the following formula in cell C8.

=DistanceInMiles(E5,E6,C8)

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (12)

  • Press the Enter button and you will see the distance between Ohio and Alaska in miles.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (13)

  • Use the following formula to see the distance in kilometers:

=DistanceInKM(E5,E6,C8)

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (14)

Here is a new APIkey that you can use to find out distances. It might get changed after a while, so check online for newer APIs if needed.

AvAMQVCQZRHasj75zfHQyX5lHvfgjJKInhHyaY6cXJtXsulSdMC3r71LGZ2uTY3a

Method 3 – Applying Excel CONCATENATE and SUBSTITUTE Functions for a Google Maps Link to Calculate the Distance Between Two Addresses

Steps:

  • Use the following formula in C8.

=CONCATENATE("http://maps.google.co.uk/maps?f=d&source=s_d&saddr=",SUBSTITUTE(B5," ","+"),"&daddr=",SUBSTITUTE(B6," ","+"))

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (15)

  • Press the Enter button and you will see the Google Map link in C8.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (16)

  • Use this link in your browser and you will get the distance information about these two addresses.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (17)

If the distance or route cannot be determined by Google, you will not get a result. You might also receive distance information for roads or transport.

How to Calculate ZIP Code Distances in Excel

  • Add theGeoDataAdd-inand necessary APIkey in Excel from the CDXGeoData Website.
  • Select the File tab, go to Options, and select Add-ins.
  • Click on the Gobutton beside theManagesection.
  • Find the downloaded Add-in and clickOK.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (18)

  • Insert the APIkey in theSettingswindow under theCDXGeodatatab which is added after installing theAdd-in.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (19)

  • Insert some valid ZIPcodes and use the formula below to calculate the distance between them.

=GeoDistance(B5,C5)

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (20)

Note:TheAPIkey will be valid for24hours and the formula can be used100times for the free version.

Practice Section

We’ve provided a practice section and a template so you can experiment with calculating distances.

How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (21)

Download the Practice Workbook

Calculate Distance Between Two Addresses.xlsx

Frequently Asked Questions

Can I calculate distances without using APIs?

It is difficult to compute distances effectively without the use of APIs since you would require access to large geographic datasets and algorithms for calculating distances based on addresses. APIs offer a more convenient and dependable solution.

Are there any Excel add-ins or plugins for calculating distances?

Third-party add-ins or plugins that interact with Excel and provide distance computation features may be available. They can streamline the process and eliminate the need for lengthy coding.

What precautions should I take when using APIs in Excel?

When using APIs, make sure to follow the API provider’s terms of service. Also, keep security in mind, especially when dealing with API keys. Keep your API keys in a secure location and avoid storing them in public files.

<< Go Back to Distance | Formula List | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
How to Calculate Distance Between Two Addresses in Excel (3 Ways) - ExcelDemy (2024)
Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 5665

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.