Need to compare Latitude & Longitude coordinates to find closest office

PaulCL

New Member
Joined
Jul 9, 2014
Messages
43
I have a list of 20,000 addresses(List A) and another list of 2,500 offices(List B) that are both geocoded. I need to determine, in miles, which of the 20,000 addresses are closest to which 2,500 offices. Is there an excel formula that would help me solve this?
Thanks!

Sample

List A
Address1CityStateZipLatitudeLongitude
119 TINE LANENEW MARKETAL3576134.83257949-86.46106284
1177 LEWIS MTN. RDGUNTERSVILLEAL3597634.42853529-86.30492038
2980 COUNTY RD 53TUSKEGEEAL3608332.47986191-85.64240964
617 CHERRY ST NWDECATURAL3560134.61583017-86.99362234
2300 HACKBERRY LANE

List B
HOOVERAL3522633.42695376-86.82373959

<colgroup><col><col><col><col><col><col></colgroup><tbody>
</tbody>
Office IDDBALatitudeLongitude
AR3021st Corner35.22692-90.78813873
AR3021st Corner35.22541-90.788153
AR3021st Corner35.82997-90.67296349
AR3021st Corner35.22169-90.82464634
AR3021st Corner35.86746-90.71383514
AR3021st Corner35.84989-90.64110695
MO306Advantage38.56485-90.38050127
MO306Advantage38.58855-90.350542
MO306Advantage38.55125-90.39158163
MO306Advantage38.60448-90.37092379

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this (use CTRL + SHIFT + ENTER while entering the formula):

List A - columns A:F
List B - columns H:K

Code:
=INDEX($A$3:$A$7,MATCH(SMALL((ABS(J3-$E$3:$E$7)^2+ABS(K3-$F$3:$F$7)^2)^(0.5),1),(ABS(J3-$E$3:$E$7)^2+ABS(K3-$F$3:$F$7)^2)^(0.5),0))

Sample Workbook:

Code:
https://app.box.com/s/uye7cps3i7q4usv6z8iuwq9wsvqzzcxc
 
Upvote 0
Very cool. But he wanted to know the distances too, so I noddled that one out.

Try this, in Kilometers (make sure it's CTRL+SHIFT+ENTER): {=INDEX(ACOS(COS(RADIANS(90-$E$3:$E$7)) *COS(RADIANS(90-J3)) +SIN(RADIANS(90-$E$3:$E$7)) *SIN(RADIANS(90-J3)) *COS(RADIANS($F$3:$F$7-K3)))*6371,MATCH(SMALL((ABS(J3-$E$3:$E$7)^2+ABS(K3-$F$3:$F$7)^2)^(0.5),1),(ABS(J3-$E$3:$E$7)^2+ABS(K3-$F$3:$F$7)^2)^(0.5),0))}

Credit is due here: BlueMM: Excel formula to calculate distance between 2 latitude, longitude (lat/lon) points (GPS positions)
 
Last edited:
Upvote 0
Try this (use CTRL + SHIFT + ENTER while entering the formula):

List A - columns A:F
List B - columns H:K

Code:
=INDEX($A$3:$A$7,MATCH(SMALL((ABS(J3-$E$3:$E$7)^2+ABS(K3-$F$3:$F$7)^2)^(0.5),1),(ABS(J3-$E$3:$E$7)^2+ABS(K3-$F$3:$F$7)^2)^(0.5),0))

Sample Workbook:

Code:
https://app.box.com/s/uye7cps3i7q4usv6z8iuwq9wsvqzzcxc

The sample workbook was right on, but I would like it in reverse (List A v List B) where the result would be Office ID.
Thanks
 
Upvote 0
This should switch the two tables:

Excel 2012
QR
3AR302395.6233
Sheet36
Cell Formulas
RangeFormula
Q3{=INDEX($H$3:$H$12,MATCH(SMALL((ABS($J$3:$J$12-E3)^2+ABS($K$3:$K$12-F3)^2)^(0.5),1),(ABS($J$3:$J$12-E3)^2+ABS($K$3:$K$12-F3)^2)^(0.5),0))}
R3{=INDEX(ACOS(COS(RADIANS(90-E3)) *COS(RADIANS(90-$J$3:$J$12)) +SIN(RADIANS(90-E3)) *SIN(RADIANS(90-$J$3:$J$12)) *COS(RADIANS(F3-$K$3:$K$12)))*6371,MATCH(SMALL((ABS($J$3:$J$12-E3)^2+ABS($K$3:$K$12-F3)^2)^(0.5),1),(ABS($J$3:$J$12-E3)^2+ABS($K$3:$K$12-F3)^2)^(0.5),0))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Thanks for the extra formulas Mr. Steel. However, when I plugged in the formulas, the Dist In Miles was a little suspect(further than expected), and since there were identical results for the first two records. Here is a better sample of my spreadsheet.
I have the list of affiliates in a separate tab called "Affiliates" with the coordinates of the affiliates. I have omitted a few columns for this post.

Formula in U2: =INDEX(Affiliates!$A$2:$A$1128,MATCH(SMALL((ABS(Affiliates!$D$2:$D$1128-S2)^2+ABS(Affiliates!$E$2:$E$1128-T2)^2)^(0.5),1),(ABS(Affiliates!$D$2:$D$1128-S2)^2+ABS(Affiliates!$E$2:$E$1128-T2)^2)^(0.5),0))
Formula in V2: =INDEX(ACOS(COS(RADIANS(90-S2)) *COS(RADIANS(90-Affiliates!$D$2:$D$1128)) +SIN(RADIANS(90-S2)) *SIN(RADIANS(90-Affiliates!$D$2:$D$1128)) *COS(RADIANS(T2-Affiliates!$E$2:$E$1128)))*6371,MATCH(SMALL((ABS(Affiliates!$D$2:$D$1128-S2)^2+ABS(Affiliates!$E$2:$E$1128-T2)^2)^(0.5),1),(ABS(Affiliates!$D$2:$D$1128-S2)^2+ABS(Affiliates!$E$2:$E$1128-T2)^2)^(0.5),0))

Address1Address2CityStateZipPhoneBirth YearBroker IDDBALatitudeLongitudeClosest AffiliateDist. In Miles
12110 JOHNS RDANCHORAGEAK995159073516078196461.110885-149.879453 WA303-0012300
134 MATTHEW PAUL WAYANCHORAGEAK995049073069132198261.22259479-149.761955 WA303-0012300
19301 VILLAGES SCENIC PARKWAYANCHORAGEAK995169072768008196161.104106-149.778328 WA303-0012294
13001 FOSTER RDANCHORAGEAK995169073069379197361.10319066-149.7478848 WA303-0012293
3412 MCKENZIE DRANCHORAGEAK995179078879176196661.18956977-149.9508385 WA303-0012307

<colgroup><col><col><col><col><col><col><col><col span="2"><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
Perhaps your issue arises because the distance is kilometers. The radius of the sphere Earth is 6371 km, or so. Convert to miles, or chains, rods, cubits, kalicams, leagues...whatever you like. Lol.
 
Upvote 0
Hey guys,

great post. i just found this and want to calculate the closest office from a list.

i have modified the formula a bit to account for my list of about 1200 any reason i would be getting a value error?

i downloaded the sample and just replaced the lat long with my values.

the formula i am using is as follows


=INDEX($A$3:$A$1200,MATCH(SMALL((ABS(J3-$E$3:$E$1200)^2+ABS(K3-$F$3:$F$1200)^2)^(0.5),1),(ABS(J3-$E$3:$E$1200)^2+ABS(K3-$F$3:$F$1200)^2)^(0.5),0))
 
Upvote 0
I see that all the formulas here require CtrlShiftEnter to invoke them. Try that and then copy the formula downward as needed.
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,405
Members
448,958
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top