Calculating shortest to longest distances between Zip Codes (using latitude and longitude)

pawcoyote

Board Regular
Joined
Mar 5, 2012
Messages
89
Office Version
  1. 365
Platform
  1. Windows
Hi, I have what I believe might be an interesting one I could use some help on.

I've have been working on formulas to show the distances (Miles) between multiple longitude and latitudes (Zip Codes). I am trying to find the shortest distance between multiple Zip Codes. Then put in order the shortest distance to longest distance. I would like to show who is assigned as the shortest distance to longest distances on the Planning worksheet. I tried to post all that I could think to help.

On the Formula worksheet I have the following formulas.

Step1: I created a Count Column that shows how many possible Zip Codes that could be included up to five (5).
Excel Formula:
=COUNTIF(TEAM!$A$2:$A$57,Planning!$E2)

Step2: I created Ref Columns 1-5 to show which Zip Code is has the shortest distance:
Excel Formula:
=IF(COLUMNS($G2:G2)>$F2,"",INDEX(TEAM!$B$2:$B$80,SMALL(IF(TEAM!$A$2:$A$80=InventoryPlanning!$E2,ROW(TEAM!$B$2:$B$80)-ROW(TEAM!$B$2)+1),COLUMNS($G2:G2))))

Step3: I created a Distance formula that will show the distances: I also noticed that the distances are estimated not actual. I have tried the Mileage number: 3963.19 and Kilometer number: 6378.135:
Excel Formula:
=IF($G2="","",IF($E2=$N2,0,ACOS(COS(RADIANS(90-$L2))*COS(RADIANS(90-$C2))+SIN(RADIANS(90-$L2))*SIN(RADIANS(90-$C2))*COS(RADIANS($M2-$D2)))*6378.135))

This formula is successfully returning the correct distances in Distance-1 thru Distance-5. But the the Ref#’s are not showing in the order of shortest distance. It is shown by the State and how I have them loaded on the TEAM worksheet. How can I change the formula above to show the shortest distance in order within the Ref Columns? I would really like to do away with looking at the State first and just look at the shortest distance between the Zip Codes (Latitude/Longitudes). That way I do not have to worry about loading States into the Team sheet just the people and their locations.

Can anyone help? Sorry for the complicated problem in advance.

Planning Worksheet:
Zip Code Distances.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACAD
1Location NumberLocation NameAddressCITYSTATEZIP CODEMin - DistMin ZipDistanceRicoh / ClientFirst NameLast NameZipRef2 - DistanceRef2 - NameRef2 - Zip CodeRef3 - DistanceRef3 - NameRef3 - Zip CodeRef4 - DistanceRef4 - NameRef4 - Zip CodeRef5 - DistanceRef5 - NameRef5 - Zip CodeSSSIGDistance Allowed: Enter Here ---->65Client Name: Enter Here ----->Customer
2123 Smith StreetMELVILLENY11747481001748SMITHANDREWSMITH10017 
3143 Fargo CTVinitaCA92606199269219JONESVINITAJONES92692JONES
413 Callen StVinitaCA92817309269230JONESVINITAJONES92692JONES
52 Box RdVinitaCA9193212592692125CustomerVINITAJONES92692JONES
6500 West StIn BetweenCA90712509140250APPLETHOMASAPPLE91402APPLE
780 Finn RdIn BetweenCA90801539269253JONESVINITAJONES92692JONES
8999 Cargo RdThomasCA90001349140234APPLETHOMASAPPLE91402APPLE
93 Blue StThomasCA93001859140285CustomerTHOMASAPPLE91402APPLE
1055 Hartford AveThomasCA91364169140216APPLETHOMASAPPLE91402APPLE
113 Providence AveThomasCA91001299140229APPLETHOMASAPPLE91402APPLE
125 June RdIn BetweenCA9236430092692300CustomerVINITAJONES92692JONES
1390 Cutler BlvdIn BetweenCA91714519140251APPLETHOMASAPPLE91402APPLE
14143 Ohio AveProvidenceRI02816260280926SMITHDAVIDSMITH02809SMITH
1599 June LNOkalahoma CityOK7310855,000  Customer    
1674 Washington StAlbuquerqueNM8710955,000  Customer    
1726 Morgan RdBessemerAL3502255,000  Customer    
Planning
Cell Formulas
RangeFormula
G2:G17G2=MIN(Formulas!O2,Formulas!S2,Formulas!W2,Formulas!AA2,Formulas!AE2)
H2:H17H2=IF($G2=Formulas!$O2,Formulas!$G2,IF($G2=Formulas!$S2,Formulas!$H2,IF($G2=Formulas!$W2,Formulas!$I2,IF($G2=Formulas!$AA2,Formulas!$J2,IF($G2=Formulas!$AE2,Formulas!$K2,"")))))
I2:I17I2=IF(G2<5000,G2,"")
J2:J17J2=IF($I2<$AB$1,$L2,$AD$1)
K2:K17K2=XLOOKUP($H2,TEAM!$B$2:$B$88,TEAM!$C$2:$C$88,"",0)
L2:L17L2=XLOOKUP($H2,TEAM!$B$2:$B$88,TEAM!$D$2:$D$88,"",0)
M2:M17M2=XLOOKUP($H2,TEAM!$B$2:$B$88,TEAM!$B$2:$B$88,"",0)
Z2:Z17Z2=XLOOKUP($H2,TERRITORY!$B$2:$B$86,TERRITORY!$D$2:$D$86,"",0)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
J:JCell Valuecontains "Customer"textNO
A:ACell ValueduplicatestextNO
I1:I17,K2:Z17,K1:M1,Z1Cell Value=""textNO


Formula Worksheet:
Zip Code Distances.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1STATEZIPCODELATLONGLAT+LONGCOUNTRef1Ref2Ref3Ref4Ref5LATLONGLAT+LONGDISTANCE-1LATLONGLAT+LONGDISTANCE-2LATLONGLAT+LONGDISTANCE-3LATLONGLAT+LONGDISTANCE-4LATLONGLAT+LONGDISTANCE-5
2NY1174740.7946-73.403-32.60845100170648219440028090286140.7517-73.9707-33.2194841.4087-73.2485-31.83987040.2778-75.2975-35.019717041.6825-71.2676-29.585120441.8814-71.356-29.4746210
3CA9260633.6951-117.8224-84.12733914029269294507  34.2262-118.447-84.22088333.6144-117.6433-84.02891937.8537-122.0229-84.1692598   55,000   55,000
4CA9281733.8512-117.7915-83.94033914029269294507  34.2262-118.447-84.22087333.6144-117.6433-84.02893037.8537-122.0229-84.1692587   55,000   55,000
5CA9193232.5783-117.1148-84.53653914029269294507  34.2262-118.447-84.220822133.6144-117.6433-84.028912537.8537-122.0229-84.1692737   55,000   55,000
6CA9071233.8512-118.1457-84.29453914029269294507  34.2262-118.447-84.22085033.6144-117.6433-84.02895337.8537-122.0229-84.1692566   55,000   55,000
7CA9100134.1912-118.1392-83.9483914029269294507  34.2262-118.447-84.22082933.6144-117.6433-84.02897937.8537-122.0229-84.1692537   55,000   55,000
8CA9236435.4667-115.2722-79.80553914029269294507  34.2262-118.447-84.220832133.6144-117.6433-84.028930037.8537-122.0229-84.1692659   55,000   55,000
9CA9171434.0197-117.9587-83.9393914029269294507  34.2262-118.447-84.22085133.6144-117.6433-84.02895437.8537-122.0229-84.1692562   55,000   55,000
10RI0281641.6914-71.5768-29.885420286102809   41.8814-71.356-29.47462841.6825-71.2676-29.585126   55,000   55,000   55,000
11OK7310835.4445-97.5619-62.11740            55,000   55,000   55,000   55,000
12NM8710935.1506-106.569-71.41840            55,000   55,000   55,000   55,000
13AL3502233.3224-86.9657-53.64330            55,000   55,000   55,000   55,000
Formulas
Cell Formulas
RangeFormula
A2:B13A2=Planning!E2
C2:C13C2=XLOOKUP(Planning!F2, ZipCodeDB!$B$2:$B$50000, ZipCodeDB!$G$2:$G$50000,,0)
D2:D13D2=XLOOKUP(Planning!$F2, ZipCodeDB!$B$2:$B$50000, ZipCodeDB!$H$2:$H$50000,,0)
E2:E13E2=IFERROR($C2+$D2,"")
F2:F13F2=COUNTIF(TEAM!$A$2:$A$57,Planning!$E2)
G2:G13G2=IF(COLUMNS($G2:G2)>$F2,"",INDEX(TEAM!$B$2:$B$80,SMALL(IF(TEAM!$A$2:$A$80=Planning!$E2,ROW(TEAM!$B$2:$B$80)-ROW(TEAM!$B$2)+1),COLUMNS($G2:G2))))
H2:H13H2=IF(COLUMNS($G2:$H2)>$F2,"",INDEX(TEAM!$B$2:$B$80,SMALL(IF(TEAM!$A$2:$A$80=Planning!$E2,ROW(TEAM!$B$2:$B$80)-ROW(TEAM!$B$2)+1),COLUMNS($G2:$H2))))
I2:I13I2=IF(COLUMNS($G2:$I2)>$F2,"",INDEX(TEAM!$B$2:$B$80,SMALL(IF(TEAM!$A$2:$A$80=Planning!$E2,ROW(TEAM!$B$2:$B$80)-ROW(TEAM!$B$2)+1),COLUMNS($G2:$I2))))
J2:J13J2=IF(COLUMNS($G2:$J2)>$F2,"",INDEX(TEAM!$B$2:$B$80,SMALL(IF(TEAM!$A$2:$A$80=Planning!$E2,ROW(TEAM!$B$2:$B$80)-ROW(TEAM!$B$2)+1),COLUMNS($G2:$J2))))
K2:K13K2=IF(COLUMNS($G2:$K2)>$F2,"",INDEX(TEAM!$B$2:$B$80,SMALL(IF(TEAM!$A$2:$A$80=Planning!$E2,ROW(TEAM!$B$2:$B$80)-ROW(TEAM!$B$2)+1),COLUMNS($G2:$K2))))
L2:L13L2=XLOOKUP($G2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$G$2:$G$50000,"",0)
M2:M13M2=XLOOKUP($G2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$H$2:$H$50000,"",0)
N2:N13N2=IF($G2="","",SUM($L2+$M2))
O2:O13O2=IF($G2="","",IF($E2=$N2,0,ACOS(COS(RADIANS(90-$L2))*COS(RADIANS(90-$C2))+SIN(RADIANS(90-$L2))*SIN(RADIANS(90-$C2))*COS(RADIANS($M2-$D2)))*6378.135))
P2:P13P2=XLOOKUP($H2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$G$2:$G$50000,"",0)
Q2:Q13Q2=XLOOKUP($H2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$H$2:$H$50000,"",0)
R2:R13R2=IF($H2="","",SUM($P2+$Q2))
S2:S13S2=IF($H2="",55000,IF($E2=$R2,0,ACOS(COS(RADIANS(90-$P2))*COS(RADIANS(90-$C2))+SIN(RADIANS(90-$P2))*SIN(RADIANS(90-$C2))*COS(RADIANS($Q2-$D2)))*6378.135))
T2:T13T2=XLOOKUP($I2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$G$2:$G$50000,"",0)
U2:U13U2=XLOOKUP($I2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$H$2:$H$50000,"",0)
V2:V13V2=IF($I2="","",SUM($T2+$U2))
W2:W13W2=IF($I2="",55000,IF($E2=$V2,0,ACOS(COS(RADIANS(90-$T2))*COS(RADIANS(90-$C2))+SIN(RADIANS(90-$T2))*SIN(RADIANS(90-$C2))*COS(RADIANS($U2-$D2)))*6378.135))
X2:X13X2=XLOOKUP($J2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$G$2:$G$50000,"",0)
Y2:Y13Y2=XLOOKUP($J2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$H$2:$H$50000,"",0)
Z2:Z13Z2=IF($J2="","",SUM($X2+$Y2))
AA2:AA13AA2=IF($J2="",55000,IF($E2=$Z2,0,ACOS(COS(RADIANS(90-$X2))*COS(RADIANS(90-$C2))+SIN(RADIANS(90-$X2))*SIN(RADIANS(90-$C2))*COS(RADIANS($Y2-$D2)))*6378.135))
AB2:AB13AB2=XLOOKUP($K2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$G$2:$G$50000,"",0)
AC2:AC13AC2=XLOOKUP($K2, ZipCodeDB!$B$2:$B$50000,ZipCodeDB!$H$2:$H$50000,"",0)
AD2:AD13AD2=IF($K2="","",SUM($AB2+$AC2))
AE2:AE13AE2=IF($K2="",55000,IF($E2=$AD2,0,ACOS(COS(RADIANS(90-$AB2))*COS(RADIANS(90-$C2))+SIN(RADIANS(90-$AB2))*SIN(RADIANS(90-$C2))*COS(RADIANS($AC2-$D2)))*6378.135))


Team Worksheet:
Book1
ABCD
1STATE COVERAGEZIPFIRST NAMELAST NAME
2AZ85283STEVENJONES
3AZ85085ROBERTSMITH
4CA91402THOMASAPPLE
5CA92692VINITAJONES
6CA94507SHANESMITH
7CO80015THERESAJONES
8CO80550CASEYSMITH
9CT02861BOBSMITH
10CT06482THOMASSMITH
11CT02809DAVIDSMITH
12GA33019BILLSMITH
13IL46311JOBSMITH
14IL60563BYRONSMITH
15IL60610STEVEJONES
16IN46311JOBSMITH
17KY41091MARCUSSMITH
18MA02861BOBSMITH
19MA02809DAVIDSMITH
20ME02861BOBSMITH
21MI48323JOELSMITH
22MI46311JOBSMITH
23MO64118LOUIESMITH
24NC28031JAMESSMITH
25NH02861BOBSMITH
26NJ19440LARRYSMITH
27NV89431JEFFREYSMITH
28NY10017ANDREWSMITH
29NY06482THOMASSMITH
30NY19440LARRYSMITH
31NY02809DAVIDSMITH
32NY02861BOBSMITH
33OH45420ANDRESMITH
34OH48323JOELSMITH
35PA19440LARRYSMITH
36PA19006DAVIDSMITH
37PA19341ERICSMITH
38PA19341BRIANSMITH
39RI02861BOBSMITH
40RI02809DAVIDSMITH
41TX77382BRANDONSMITH
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D1Cell ValueduplicatestextNO
 
No problem...just post back. Glad you are feeling better.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This is what my question was getting at. Given some baseline location whose lat/long are shown in column B of the Formulas worksheet, you want to determine which are the five closest zip codes that appear in the Team worksheet. And the ZipCodeDB worksheet is needed as a cross reference to provide lat/long for any zip codes in the Team worksheet. Is that correct? That is what the formula I offered earlier does. Have a look at the results returned. If that is what you want, then the output of that array can be adjusted to deliver just the content desired. You don't need the Formulas worksheet at all, as the results can be obtained using the input zip code in column F of the Planning worksheet, and then after developing the results that depend on the ZipCodeDB and TEAM worksheets, the final results can be spilled across a row in the Planning worksheet, as shown in the slightly reworked version below. Then some other formulas in the columns G:J range may not be needed (note that the min formula in column G isn't referencing the correct cells right now).
MrExcel_20240131_C (version 1).xlsx
CDEFGHIJKLMNOPQRSTUVWXYZAAABACAD
1AddressCITYSTATEZIP CODEMin - DistMin ZipDistanceRicoh / ClientDistanceFirst NameLast NameZipDistanceFirst NameLast Name ZipDistanceFirst NameLast Name ZipDistanceFirst NameLast Name ZipDistanceFirst NameLast Name Zip
2123 Smith StreetMELVILLENY1174769.578899648269.578899Customer48.09604ANDREWSMITH1001769.578899THOMASSMITH6482170.28655LARRYSMITH19440204.25017DAVIDSMITH2809209.53438BOBSMITH2861
Planning
Cell Formulas
RangeFormula
G2G2=MIN(Formulas!O2,Formulas!S2,Formulas!W2,Formulas!AA2,Formulas!AE2)
H2H2=IF($G2=Formulas!$O2,Formulas!$G2,IF($G2=Formulas!$S2,Formulas!$H2,IF($G2=Formulas!$W2,Formulas!$I2,IF($G2=Formulas!$AA2,Formulas!$J2,IF($G2=Formulas!$AE2,Formulas!$K2,"")))))
I2I2=IF(G2<5000,G2,"")
J2J2=IF($I2<$AG$1,$M2,$AI$1)
K2:AD2K2=LET(dbzip,ZipCodeDB!$B$2:$B$50000, dblat,ZipCodeDB!$G$2:$G$50000, dblong,ZipCodeDB!$H$2:$H$50000, basezip,F2, baselat,XLOOKUP(basezip,dbzip,dblat), baselong,XLOOKUP(basezip,dbzip,dblong), zip,TEAM!$B$2:$B$80, fname,XLOOKUP(zip,TEAM!$B$2:$B$80,TEAM!$C$2:$C$80), lname,XLOOKUP(zip,TEAM!$B$2:$B$80,TEAM!$D$2:$D$80), lat,XLOOKUP(zip,dbzip,dblat), long,XLOOKUP(zip,dbzip,dblong), dist,IFERROR(ACOS(COS(RADIANS(90-lat))*COS(RADIANS(90-baselat))+SIN(RADIANS(90-lat))*SIN(RADIANS(90-baselat))*COS(RADIANS(long-baselong)))*6378.135,""), res,TOROW(TAKE(SORT(UNIQUE(HSTACK(dist,fname,lname,zip)),1,1),5)), res)
Dynamic array formulas.
I brought the code into Column K and it spills across K-AD. Looks really good. Looking at how to update G-H now. Also, to change to Mile I just need to update the calculation to be 3963.19 instead of the 6378.135, correct?
 
Upvote 0
One thing I am seeing is if the Zip Code in G is either blank or has a 00000, it shows nothing in K-N but it will add people in all the other cell for O-AD. Looking into how to show nothing in the cells if the Zip Code in G is either not there or listed as 00000.

Excel Formula:
=LET(dbzip,ZipCodeDB!$B$2:$B$50000, dblat,ZipCodeDB!$G$2:$G$50000, dblong,ZipCodeDB!$H$2:$H$50000,
basezip,F48, baselat,XLOOKUP(basezip,dbzip,dblat), baselong,XLOOKUP(basezip,dbzip,dblong),
zip,COETeam!$B$2:$B$69, fname,XLOOKUP(zip,COETeam!$B$2:$B$69,COETeam!$C$2:$C$69), lname,XLOOKUP(zip,COETeam!$B$2:$B$69,COETeam!$D$2:$D$69),
lat,XLOOKUP(zip,dbzip,dblat), long,XLOOKUP(zip,dbzip,dblong),
dist,IFERROR(ACOS(COS(RADIANS(90-lat))*COS(RADIANS(90-baselat))+SIN(RADIANS(90-lat))*SIN(RADIANS(90-baselat))*COS(RADIANS(long-baselong)))*3963.19,""),
res,TOROW(TAKE(SORT(UNIQUE(HSTACK(dist,fname,lname,zip)),1,1),5)), res)
 
Upvote 0
@KRice

I updated the code and it looks to be working nicely. The only thing is when there is nothing or 00000 in Column F the spill cells bring in data that should not be there. I would like it to show blanks unless a Zip Code is entered into column F. J - M work fine.. Any suggestions?
COE_Zip Code Distance Locations and Planning DRAFT021724.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1Location NumberLocation NameAddressCITYSTATEZIP CODEMin - DistMin ZipRicoh / ClientRef1 - DistanceRef1 - First NameRef1 - Last NameRef1 - Zip CodeRef2 - DistanceRef2 - First NameRef2 - Last NameRef2 - Zip CodeRef3 - DistanceReft3 - First NameRef3 - Last NameRef3 - Zip CodeRef4 - DistanceRef4 - First NameRef4 - Last NameRef4 - Zip CodeRef5 - DistanceRef5 - First NameRef5 - Last NameRef5 - Zip CodeSSSIGDistance Allowed: Enter Here ---->65Client Name: Enter Here ----->Customer
2123 Smith StreetMELVILLENY117473010017SMITH30ANDREWSMITH1001743THOMASSMITH0648299DAVIDSMITH19006106LARRYSMITH19440127DAVIDSMITH02809 
3143 Fargo CTVinitaCA926061292692JONES12VINITAJONES9269251THOMASAPPLE91402330ROBERTSMITH85085340STEVENJONES85283372SHANESMITH94507JONES
413 Callen StVinitaCA928171892692JONES18VINITAJONES9269246THOMASAPPLE91402328ROBERTSMITH85085339STEVENJONES85283365SHANESMITH94507JONES
52 Box RdVinitaCA919327892692Customer78VINITAJONES92692138THOMASAPPLE91402302ROBERTSMITH85085306STEVENJONES85283458SHANESMITH94507JONES
6143 Ohio AveProvidenceRI02904502861SMITH5BOBSMITH0286115DAVIDSMITH0280999THOMASSMITH06482152ANDREWSMITH10017224DAVIDSMITH19006SMITH
799 June LNOkalahoma CityRI02703402861SMITH4BOBSMITH0286117DAVIDSMITH02809107THOMASSMITH06482161ANDREWSMITH10017232DAVIDSMITH19006SMITH
874 Washington StAlbuquerqueNM8710932580015Customer325THERESAJONES80015329ROBERTSMITH85085330STEVENJONES85283380CASEYSMITH80550580TERRANCESMITH76065JONES
90000000000000005265DAVIDSMITH028095273BOBSMITH028615362THOMASSMITH064825390ANDREWSMITH10017
100000000000000005265DAVIDSMITH028095273BOBSMITH028615362THOMASSMITH064825390ANDREWSMITH10017 
110000000000000005265DAVIDSMITH028095273BOBSMITH028615362THOMASSMITH064825390ANDREWSMITH10017
Planning
Cell Formulas
RangeFormula
G2:G11G2=MIN(Planning!J2, Planning!N2, Planning!R2, Planning!V2, Planning!Z2)
H2:H11H2=IF($G2=Planning!$J2, Planning!$M2, IF($G2=Planning!$N2, Planning!$Q2, IF($G2=Planning!$R2, Planning!$U2, IF($G2=Planning!$V2, Planning!$Y2, IF($G2=Planning!$Z2, Planning!$AC2, "") ) ) ) )
I2:I11I2=IF($G2<$AF$1,$L2,$AH$1)
J2:AC11J2=LET(dbzip,ZipCodeDB!$B$2:$B$50000, dblat,ZipCodeDB!$G$2:$G$50000, dblong,ZipCodeDB!$H$2:$H$50000, basezip,F2, baselat,XLOOKUP(basezip,dbzip,dblat), baselong,XLOOKUP(basezip,dbzip,dblong), zip,TEAM!$B$2:$B$65, fname,XLOOKUP(zip,TEAM!$B$2:$B$65,TEAM!$C$2:$C$65), lname,XLOOKUP(zip,TEAM!$B$2:$B$65,TEAM!$D$2:$D$65), lat,XLOOKUP(zip,dbzip,dblat), long,XLOOKUP(zip,dbzip,dblong), dist,IFERROR(ACOS(COS(RADIANS(90-lat))*COS(RADIANS(90-baselat))+SIN(RADIANS(90-lat))*SIN(RADIANS(90-baselat))*COS(RADIANS(long-baselong)))*3963.19,""), res,TOROW(TAKE(SORT(UNIQUE(HSTACK(dist,fname,lname,zip)),1,1),5)), res)
AD2:AD8,AD10AD2=XLOOKUP($H2,TERRITORY!$B$2:$B$86,TERRITORY!$D$2:$D$86,"",0)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
I:JCell Valuecontains "Customer"textNO
A:ACell ValueduplicatestextNO
K1:M1,AD1,K2:AD11Cell Value=""textNO
 
Upvote 0
Sounds as if a simple check for an input is needed…should be easy to address. I’ll have a look when I have a moment at the computer. Have you noted any other issues? Are there any instances where a ZIP code might be entered that does not exist in the DB reference?
 
Upvote 0
Sounds as if a simple check for an input is needed…should be easy to address. I’ll have a look when I have a moment at the computer. Have you noted any other issues? Are there any instances where a ZIP code might be entered that does not exist in the DB reference?
Thanks and not as of yet. So far I have been running a lot of addresses to see what happens.
 
Upvote 0
A comment and a question:
The Min Distance/ZIP will always be found in the Ref1 columns J/M because the main formula that spills the results uses a SORT function to sort all of the computed distances by distance, and then to "TAKE" only the smallest 5 in that order. So the col G & H formulas are not necessary. You could rely on cols J & M, or replace the G & H formula with simpler =J2 and =M2 formulas.

You mention something about column F either being blank or 00000. How are you making "00000" appear? Is that done with custom/special formatting, perhaps using Excel's built-in ZIP code format offering? If so, then the ZIP codes are numeric and not text. I just wanted to confirm that there is consistency between your large DB table, and any other ZIP codes used. An easy check can be made for a cell with the Special ZIP formatting...entering a single 0 results in 00000. For testing purposes, I've assumed this is the case and applied special formatting for "ZIP code" for all ZIP fields on the Planning and mocked-up TEAM and ZipCodeDB worksheets.

Now something interesting happens, depending on what is entered in column F. If 0 is entered with the special ZIP formatting, we'll see 00000, and because XLOOKUP cannot find such a ZIP, an #N/A error is returned, so we will not have a baseline lat/long (only #N/A errors for each), and other functions that depend on the baseline lat/long will fail to return anything meaningful. In this case, the distance array returned by the formula is all blanks, so there is nothing to sort, and the names displayed are the first five in the list on the TEAM worksheet.

However, if nothing is entered in column F, we'll see nothing...a blank cell...,because the lookup range in the XLOOKUP functions is larger than the bottom row where data are found, XLOOKUP finds the first blank below the last ZIP code and returns the corresponding lat/long, which are also blank. These blanks are interpreted as 0's, so the baseline lat/long for blank ZIP codes is 0/0, a location sometimes called Null Island in the Gulf of Guinea of the coast of West Africa. A weather buoy called the Soul buoy is moored there. So all distances are computed relative to this location in the middle of the ocean!

But there is one more scenario to consider: perhaps a value is entered in column F that does not match any ZIP codes in ZipCodeDB. That also results in an #N/A error and the results are the same as the first scenario above.

None of these scenarios should result in a meaningful result, so some error trapping is appropriate. I've added this to the result part of the formula: IF(OR(basezip=0,ISERROR(baselat))
...followed by an expression to deliver a blank row if TRUE: EXPAND("",,20,""). The basezip component addresses the first scenario and the ISERROR component addresses the other two.
MrExcel_20240131_C (version 1).xlsx
DEFGHIJKLMNOPQ
1CITYSTATEZIP CODEMin - DistMin ZipRicoh / ClientRef1 - DistanceRef1 - First NameRef1 - Last NameRef1 - ZipRef2 - DistanceRef2 - First NameRef2 - Last Name Ref2 - Zip
2MELVILLENY117474810017SMITH48ANDREWSMITH1001770THOMASSMITH06482
3VinitaCA926061992692JONES19VINITAJONES9269283THOMASAPPLE91402
4VinitaCA928173092692JONES30VINITAJONES9269273THOMASAPPLE91402
5VinitaCA9193212592692Customer125VINITAJONES92692221THOMASAPPLE91402
6In BetweenCA907125091402APPLE50THOMASAPPLE9140253VINITAJONES92692
7In BetweenCA00000  Customer 
8ThomasCA  Customer 
9ThomasCA93001  Customer 
10ThomasCA91364  Customer 
11ThomasCA910012991402APPLE29THOMASAPPLE9140279VINITAJONES92692
12In BetweenCA9236430092692Customer300VINITAJONES92692321THOMASAPPLE91402
13In BetweenCA917145191402APPLE51THOMASAPPLE9140254VINITAJONES92692
14ProvidenceRI028162602809SMITH26DAVIDSMITH0280928BOBSMITH02861
15Okalahoma CityOK73108185092692Customer1850VINITAJONES926921910THOMASAPPLE91402
16AlbuquerqueNM87109103192692Customer1031VINITAJONES926921091THOMASAPPLE91402
17BessemerAL35022129519440Customer1295LARRYSMITH194401418ANDREWSMITH10017
Planning
Cell Formulas
RangeFormula
G2:G17G2=J2
H2:H17H2=M2
I2:I17I2=IF($G2<$AF$1,$L2,$AH$1)
J2:AC17J2=LET(dbzip,ZipCodeDB!$B$2:$B$50000, dblat,ZipCodeDB!$G$2:$G$50000, dblong,ZipCodeDB!$H$2:$H$50000, basezip,F2, baselat,XLOOKUP(basezip,dbzip,dblat), baselong,XLOOKUP(basezip,dbzip,dblong), zip,TEAM!$B$2:$B$80, fname,XLOOKUP(zip,TEAM!$B$2:$B$80,TEAM!$C$2:$C$80), lname,XLOOKUP(zip,TEAM!$B$2:$B$80,TEAM!$D$2:$D$80), lat,XLOOKUP(zip,dbzip,dblat), long,XLOOKUP(zip,dbzip,dblong), dist,IFERROR(ROUND(ACOS(COS(RADIANS(90-lat))*COS(RADIANS(90-baselat))+SIN(RADIANS(90-lat))*SIN(RADIANS(90-baselat))*COS(RADIANS(long-baselong)))*6378.135,0),""), res,IF(OR(basezip=0,ISERROR(baselat)),EXPAND("",,20,""),TOROW(TAKE(SORT(UNIQUE(HSTACK(dist,fname,lname,zip)),1,1),5))), res )
Dynamic array formulas.
 
Upvote 0
Solution
A comment and a question:
The Min Distance/ZIP will always be found in the Ref1 columns J/M because the main formula that spills the results uses a SORT function to sort all of the computed distances by distance, and then to "TAKE" only the smallest 5 in that order. So the col G & H formulas are not necessary. You could rely on cols J & M, or replace the G & H formula with simpler =J2 and =M2 formulas.

You mention something about column F either being blank or 00000. How are you making "00000" appear? Is that done with custom/special formatting, perhaps using Excel's built-in ZIP code format offering? If so, then the ZIP codes are numeric and not text. I just wanted to confirm that there is consistency between your large DB table, and any other ZIP codes used. An easy check can be made for a cell with the Special ZIP formatting...entering a single 0 results in 00000. For testing purposes, I've assumed this is the case and applied special formatting for "ZIP code" for all ZIP fields on the Planning and mocked-up TEAM and ZipCodeDB worksheets.

Now something interesting happens, depending on what is entered in column F. If 0 is entered with the special ZIP formatting, we'll see 00000, and because XLOOKUP cannot find such a ZIP, an #N/A error is returned, so we will not have a baseline lat/long (only #N/A errors for each), and other functions that depend on the baseline lat/long will fail to return anything meaningful. In this case, the distance array returned by the formula is all blanks, so there is nothing to sort, and the names displayed are the first five in the list on the TEAM worksheet.

However, if nothing is entered in column F, we'll see nothing...a blank cell...,because the lookup range in the XLOOKUP functions is larger than the bottom row where data are found, XLOOKUP finds the first blank below the last ZIP code and returns the corresponding lat/long, which are also blank. These blanks are interpreted as 0's, so the baseline lat/long for blank ZIP codes is 0/0, a location sometimes called Null Island in the Gulf of Guinea of the coast of West Africa. A weather buoy called the Soul buoy is moored there. So all distances are computed relative to this location in the middle of the ocean!

But there is one more scenario to consider: perhaps a value is entered in column F that does not match any ZIP codes in ZipCodeDB. That also results in an #N/A error and the results are the same as the first scenario above.

None of these scenarios should result in a meaningful result, so some error trapping is appropriate. I've added this to the result part of the formula: IF(OR(basezip=0,ISERROR(baselat))
...followed by an expression to deliver a blank row if TRUE: EXPAND("",,20,""). The basezip component addresses the first scenario and the ISERROR component addresses the other two.
MrExcel_20240131_C (version 1).xlsx
DEFGHIJKLMNOPQ
1CITYSTATEZIP CODEMin - DistMin ZipRicoh / ClientRef1 - DistanceRef1 - First NameRef1 - Last NameRef1 - ZipRef2 - DistanceRef2 - First NameRef2 - Last Name Ref2 - Zip
2MELVILLENY117474810017SMITH48ANDREWSMITH1001770THOMASSMITH06482
3VinitaCA926061992692JONES19VINITAJONES9269283THOMASAPPLE91402
4VinitaCA928173092692JONES30VINITAJONES9269273THOMASAPPLE91402
5VinitaCA9193212592692Customer125VINITAJONES92692221THOMASAPPLE91402
6In BetweenCA907125091402APPLE50THOMASAPPLE9140253VINITAJONES92692
7In BetweenCA00000  Customer 
8ThomasCA  Customer 
9ThomasCA93001  Customer 
10ThomasCA91364  Customer 
11ThomasCA910012991402APPLE29THOMASAPPLE9140279VINITAJONES92692
12In BetweenCA9236430092692Customer300VINITAJONES92692321THOMASAPPLE91402
13In BetweenCA917145191402APPLE51THOMASAPPLE9140254VINITAJONES92692
14ProvidenceRI028162602809SMITH26DAVIDSMITH0280928BOBSMITH02861
15Okalahoma CityOK73108185092692Customer1850VINITAJONES926921910THOMASAPPLE91402
16AlbuquerqueNM87109103192692Customer1031VINITAJONES926921091THOMASAPPLE91402
17BessemerAL35022129519440Customer1295LARRYSMITH194401418ANDREWSMITH10017
Planning
Cell Formulas
RangeFormula
G2:G17G2=J2
H2:H17H2=M2
I2:I17I2=IF($G2<$AF$1,$L2,$AH$1)
J2:AC17J2=LET(dbzip,ZipCodeDB!$B$2:$B$50000, dblat,ZipCodeDB!$G$2:$G$50000, dblong,ZipCodeDB!$H$2:$H$50000, basezip,F2, baselat,XLOOKUP(basezip,dbzip,dblat), baselong,XLOOKUP(basezip,dbzip,dblong), zip,TEAM!$B$2:$B$80, fname,XLOOKUP(zip,TEAM!$B$2:$B$80,TEAM!$C$2:$C$80), lname,XLOOKUP(zip,TEAM!$B$2:$B$80,TEAM!$D$2:$D$80), lat,XLOOKUP(zip,dbzip,dblat), long,XLOOKUP(zip,dbzip,dblong), dist,IFERROR(ROUND(ACOS(COS(RADIANS(90-lat))*COS(RADIANS(90-baselat))+SIN(RADIANS(90-lat))*SIN(RADIANS(90-baselat))*COS(RADIANS(long-baselong)))*6378.135,0),""), res,IF(OR(basezip=0,ISERROR(baselat)),EXPAND("",,20,""),TOROW(TAKE(SORT(UNIQUE(HSTACK(dist,fname,lname,zip)),1,1),5))), res )
Dynamic array formulas.
Hi,
The 00000 is if a zero is added by mistake into column F it the formatting will make it a Zip Code format which is 00000. Never knew about the Bouy that is interesting. Hate to ask this but would this have been easier to build in VBA? I am going to add the new code and it to my sheet and see what happens. Will let you know. Thank you for all the help and guidance.
 
Upvote 0
Thanks for the update. VBA is always an option, although for some, that may be problem (e.g., IT restrictions that limit active code, or the skill set needed for implementing and maintaining the code). In this case, the somewhat complicated looking formula isn't too difficult to follow: several variable assignments based on your worksheet structure, lookups to obtain the lat/long for the baseline location, lookup to obtain the lat/long for known "team" zip codes, and then a distance calculation for every team member's location relative to the baseline location, followed by a final formula that sorts the results, takes the shortest five, and formats the results for output.

Now that I've looked at this again, there is an easier way to make the assignments for the first name (fname) and last name (lname) arrays. The revised formula:
Excel Formula:
=LET(dbzip,ZipCodeDB!$B$2:$B$50000, dblat,ZipCodeDB!$G$2:$G$50000, dblong,ZipCodeDB!$H$2:$H$50000, team,TEAM!$B$2:$D$80,
basezip,F3, baselat,XLOOKUP(basezip,dbzip,dblat), baselong,XLOOKUP(basezip,dbzip,dblong),
zip,CHOOSECOLS(team,1), fname,CHOOSECOLS(team,2), lname,CHOOSECOLS(team,3),
lat,XLOOKUP(zip,dbzip,dblat), long,XLOOKUP(zip,dbzip,dblong),
dist,IFERROR(ROUND(ACOS(COS(RADIANS(90-lat))*COS(RADIANS(90-baselat))+SIN(RADIANS(90-lat))*SIN(RADIANS(90-baselat))*COS(RADIANS(long-baselong)))*6378.135,0),""),
res,IF(OR(basezip=0,ISERROR(baselat)),EXPAND("",,20,""),TOROW(TAKE(SORT(UNIQUE(HSTACK(dist,fname,lname,zip)),1,1),5))), res )
 
Upvote 0
=LET(dbzip,ZipCodeDB!$B$2:$B$50000, dblat,ZipCodeDB!$G$2:$G$50000, dblong,ZipCodeDB!$H$2:$H$50000, team,TEAM!$B$2:$D$80, basezip,F3, baselat,XLOOKUP(basezip,dbzip,dblat), baselong,XLOOKUP(basezip,dbzip,dblong), zip,CHOOSECOLS(team,1), fname,CHOOSECOLS(team,2), lname,CHOOSECOLS(team,3), lat,XLOOKUP(zip,dbzip,dblat), long,XLOOKUP(zip,dbzip,dblong), dist,IFERROR(ROUND(ACOS(COS(RADIANS(90-lat))*COS(RADIANS(90-baselat))+SIN(RADIANS(90-lat))*SIN(RADIANS(90-baselat))*COS(RADIANS(long-baselong)))*6378.135,0),""), res,IF(OR(basezip=0,ISERROR(baselat)),EXPAND("",,20,""),TOROW(TAKE(SORT(UNIQUE(HSTACK(dist,fname,lname,zip)),1,1),5))), res )
This worked great as well. Change F3 to F2 and it worked great. You changed to look at the Columns on the team sheet that is neat. I have always wondered if what is cleaner, formulas or VBA. I am learning about VBA as well. I also changed from kilometers to miles in the RADIANS calculation.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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