Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Calculating the Distance between Points.

Calculating the Distance between Points

Written by Allen Wyatt (last updated October 22, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


Mike tracks latitude and longitude values in an Excel worksheet. As these are essentially points on a grid, Mike would like to calculate the distance between any two given latitude/longitude points.

If the latitude and longitude pairs were really just points on a grid, then calculating the distance between them would be easy. The problem is that they are really points on a sphere, which means that you can't use flat-grid calculations to determine distance. In addition, there are many ways that you can calculate distances: shortest surface distance, optimum flight path ("as the crow flies"), distance through the earth, driving distance, etc.

Obviously this could be a complicated question. In the space available, I'll examine a couple of ways to determine the great circle distance ("as the crow flies"), and then provide some references for additional information on the other types of calculations.

The first thing you need to figure out is how the latitude and longitude of each point will be represented in Excel. There are several ways it could be represented. For instance, you could enter the degrees, minutes, and seconds in individual cells. Or, you could have them in a singe cell as DD:MM:SS. Either way is acceptable, but they will need to be treated differently your formulas. Why? Because if you enter latitude and longitude as DD:MM:SS, then Excel will convert them internally into a time value, and you just need to take that conversion into account.

What you are going to need to do, no matter what, is convert your latitude and longitude into a decimal value in radians. If you have a coordinate in three separate cells (degrees, minutes, and seconds), then you can use the following formula to do the conversion to a decimal value in radians:

=RADIANS((Degrees*3600+Minutes*60+Seconds)/3600)

The formula uses named ranges for your degrees, minutes, and seconds. It converts those three values into a single value representing total degrees, and then uses the RADIANS function to convert this to radians. If you start with a value of 32 degrees, 48 minutes, and 0 seconds, the formula ends up looking like this:

=RADIANS((32*3600+48*60+0)/3600)
=RADIANS((115200+2880+0)/3600)
=RADIANS(118080/3600)
=RADIANS(32.8)
=0.572467995

If you are storing your coordinates in the format of DD:MM:SS in a single cell (in this example, cell E12), then you can use the following formula to convert to a decimal value in radians:

=RADIANS((DAY(E12)*86400+HOUR(E12)*3600+MINUTE(E12)*60+SECOND(E12))/3600)

Assuming that cell E12 contains 32:48:00, then the formula ends up looking like this:

=RADIANS((1*86400+8*3600+48*60+0)/3600)
=RADIANS((86400+28800+2880+0)/3600)
=RADIANS(118080/3600)
=RADIANS(32.8)
=0.572467995

With your coordinates in radians, you can use a trigonometric formula to calculate distance along the surface of a sphere. There are many such formulas that could be used; the following formula will suffice for our purposes:

=ACOS(SIN(Lat1)*SIN(Lat2)+COS(Lat1)*COS(Lat2)*COS(Lon2-Lon1))*180/PI()*60

In this formula, each of the latitude (Lat1 and Lat2) and longitude (Lon1 and Lon2) coordinates must be a decimal value, in radians, as already discussed. The formula returns a value in nautical miles, which you can then apply various formulas to in order to convert to other units of measure, as desired.

You should realize that the values you come up with by using any formula that calculates distance on the surface of a sphere will give slightly erroneous results. Why? Because the Earth is not a perfect sphere. Thus, the distances should only be considered approximate. If you want to get a bit more accurate, then you can use the following formula to determine your nautical miles:

=ACOS(SIN(Lat1)*SIN(Lat2)+COS(Lat1)*COS(Lat2)*COS(Lon2-Lon1))*3443.89849

This formula substitutes the radius of the earth (3443.89849 nautical miles) for the radius of a sphere (180/PI()*60, or 3437.746771). Either way, the answer should still be considered approximate.

As you can tell, the formula to calculate distances is quite long. You may find it easier to develop your own user-defined function that will do the calculation for you. The following function takes four values (the two pairs of latitudes and longitudes, in degrees), and then returns a result in nautical miles:

Function CrowFlies(dlat1, dlon1, dlat2, dlon2)
    Pi = Application.Pi()
    earthradius = 3443.89849  'nautical miles

    lat1 = dlat1 * Pi / 180
    lat2 = dlat2 * Pi / 180
    lon1 = dlon1 * Pi / 180
    lon2 = dlon2 * Pi / 180

    cosX = Sin(lat1) * Sin(lat2) + Cos(lat1) _
      * Cos(lat2) * Cos(lon1 - lon2)
    CrowFlies = earthradius * Application.Acos(cosX)
End Function

If you would like to see a more in-depth discussion of latitudes and longitudes, and the math involved, you can find a good selection of articles at this site:

http://mathforum.org/library/drmath/sets/select/dm_lat_long.html

With the math under your belt, then you can start to look about at various formulas you can use. There is an interesting one in VBA at this Web page:

http://www.freevbcode.com/ShowCode.asp?ID=5532

A good general-purpose discussion can also be found at Chip Pearson's site, here:

http://www.cpearson.com/excel/LatLong.aspx

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3275) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Calculating the Distance between Points.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Counting the Number of Blank Cells

If you need to count the number of blank cells in a range, the function to use is COUNTBLANK. This tip discusses the ...

Discover More

Changing Macro Cell References Based on Edits

Place a cell reference in a macro, modify the structure of your worksheet, and you may soon find that the cell reference ...

Discover More

Printing Images Based on Hidden Text Setting

When you print your document, the images in the document are normally printed. What if you want only some of your images ...

Discover More

Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!

More ExcelTips (menu)

Determining "Highest Since" or "Lowest Since"

When compiling statistics on a collection of data points, you may want to know whether a particular value is the "highest ...

Discover More

Evaluating Formulas

Need a bit of help in figuring out how Excel is evaluating a particular formula? It's easy to figure out if you use the ...

Discover More

Returning the Smallest Non-Zero Value

In a series of values you may need to know the smallest value that isn't a zero. There is no built-in function to do ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine more than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.