To calculate the distance between two sets of latitude and longitude coordinates in Excel, you must use the Haversine formula. Because Excel does not have a built-in "distance" function, you need to combine trigonometric functions (SIN, COS, ATAN2) and convert your degrees into radians using the RADIANS function. This method calculates the "great-circle distance," which is the shortest distance between two points on the surface of a sphere.
What You'll Need
- Microsoft Excel (any modern version).
- Two sets of coordinates: Latitude and Longitude for Point A and Point B.
- Coordinates must be in Decimal Degrees format (e.g., 34.0522, -118.2437) rather than Degrees/Minutes/Seconds.
Step-by-Step
- Organize Your Data: Set up your spreadsheet so that your coordinates are in separate columns. For example:
- Cell A2: Latitude of Point 1
- Cell B2: Longitude of Point 1
- Cell C2: Latitude of Point 2
- Cell D2: Longitude of Point 2
- Choose Your Unit of Measurement: The formula requires a constant for the Earth's radius. Decide if you want the result in kilometers or miles:
- For Kilometers: Use 6371
- For Miles: Use 3959
- Enter the Haversine Formula: Copy and paste the following formula into cell E2. This example uses 6371 for kilometers. If you prefer miles, replace 6371 with 3959.
=ACOS(COS(RADIANS(90-A2))*COS(RADIANS(90-C2))+SIN(RADIANS(90-A2))*SIN(RADIANS(90-C2))*COS(RADIANS(B2-D2)))*6371 - Alternative High-Precision Formula: If you are calculating very short distances or require higher mathematical precision, use the more robust version of the Haversine formula:
=2*6371*ASIN(SQRT(SIN(RADIANS((C2-A2)/2))^2+COS(RADIANS(A2))*COS(RADIANS(C2))*SIN(RADIANS((D2-B2)/2))^2)) - Apply to All Rows: Click the bottom-right corner of cell E2 (the fill handle) and drag it down to apply the calculation to all other pairs of coordinates in your list.
- Format the Result: The resulting number is a raw decimal. Right-click the cell, select Format Cells, choose Number, and set the decimal places to 2 for better readability.
Common Mistakes
- Using Degrees instead of Radians: Excel's trigonometric functions (SIN, COS, TAN) only work with radians. If you forget to wrap your coordinates in the
RADIANS()function, your results will be wildly incorrect. - Mixing Up Lat and Long: Ensure you aren't accidentally putting longitude in the latitude column. Latitude (North/South) always comes first in standard coordinate pairs.
- Incorrect Signage: Ensure your West longitudes and South latitudes are entered as negative numbers. For example, New York City is roughly 40.7128 N (positive) and 74.0060 W (negative). If you enter West as a positive number, Excel will calculate the distance to a point on the opposite side of the globe.
- Using DMS Format: If your data looks like 34° 3' 12", the formula will not work. You must convert these to decimals first (Degrees + Minutes/60 + Seconds/3600).
Tips
- Create a Named Constant: Instead of typing 6371 directly into every formula, put the value 6371 in a single cell (e.g., $Z$1) and reference that cell in your formula (e.g.,
...)*$Z$1). This allows you to switch between miles and kilometers for your entire sheet by changing just one cell. - Data Validation: Use Data Validation (Data tab > Data Validation) to ensure latitudes stay between -90 and 90, and longitudes stay between -180 and 180 to prevent typos from breaking the formula.
- Handling Empty Cells: If some of your rows are empty, the formula will return an error. Wrap your formula in an IFERROR function to keep the sheet clean:
=IFERROR([your_formula], "").
Troubleshooting
- The formula returns #NUM!: This usually happens when the result of the internal calculation is slightly greater than 1 or less than -1 due to floating-point rounding errors, which
ACOScannot process. To fix this, use the second "High-Precision" formula provided in the step-by-step section, as it usesASINandSQRT, which are more stable. - The distance seems impossibly large: Double-check your signs. If one point is -74 (West) and the other is 74 (East), Excel will calculate the distance across the entire world. Ensure both are negative if they are both in the Western Hemisphere.
- The result is 0: Check that you haven't accidentally referenced the same cell twice (e.g., referencing A2 for both the start and end latitude).
Frequently Asked Questions
Does this formula account for the Earth's curvature?
Yes. The Haversine formula specifically calculates the distance over the surface of a sphere, making it far more accurate for long distances than a straight-line Pythagorean calculation.
Is the Earth a perfect sphere?
No, the Earth is an oblate spheroid (slightly flattened at the poles). While the Haversine formula assumes a sphere, the margin of error is typically less than 0.5%, which is sufficient for most business and general mapping purposes.
How do I convert Degrees, Minutes, Seconds (DMS) to Decimal Degrees?
To convert DMS to decimal, use the formula: Decimal = Degrees + (Minutes/60) + (Seconds/3600). If the direction is South or West, multiply the final result by -1.
Can I use this formula for very short distances, like across a street?
Yes, but for extremely short distances (under 100 meters), the precision of your coordinate data (the number of decimal places) matters more than the formula used.
Which is better: the ACOS formula or the ASIN formula?
The ASIN (Haversine) version is generally preferred because it is more numerically stable. The ACOS version can fail (returning #NUM!) when calculating the distance between two points that are very close together.