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
 
Glad to hear. Yes, many of the variable assignments inside the LET function are not strictly necessary; however, doing so (e.g., in the case of baselat, baselong, zip, fname, lname) makes the formula easier to understand, and in cases where those values are needed several times, redundancy of some formulas can be reduced.
 
Upvote 1

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Glad to hear. Yes, many of the variable assignments inside the LET function are not strictly necessary; however, doing so (e.g., in the case of baselat, baselong, zip, fname, lname) makes the formula easier to understand, and in cases where those values are needed several times, redundancy of some formulas can be reduced.
Going to mark this as complete. Going to see if I can build this within VBA as well. Learning as I move forward. I greatly appreciate all your help!
 
Upvote 0
Happy to help…thanks for the feedback.
Hi, I am working on trying to put this into VBA would I post any help requests under this post since it is almost a part of this question, or would I have to create a totally new post? Thanks for your guidance. I do not want to break the rules of the forum.
 
Upvote 0
I would recommend beginning a new post and mention in it that you previously worked through the forum to develop a formula-based solution to this problem, but now you'd like some help achieving a similar output using VBA...and mention VBA in the post title, and link to this thread in the body of your message (click on the line-node icon in the upper right of the most appropriate post number to get the URL for it). The reasons?...a new post is more likely to draw fresh eyes upon it, and by clarifying that you want help with VBA, you'll have a better chance getting expertise in that area. Additionally, your original post included a lot of formulas and a Formulas worksheet that have been eliminated, and the emphasis on looking at States has been resolved...both of which could cause confusion if someone tries to work through this thread.

So I think a fresh post would be appropriate, but be sure to include the necessary minisheets to create a working example. In addition to posting a small example of the Planning worksheet displaying the desired results, also include small examples of the TEAM worksheet and ZipCodeDB (I needed to construct a worksheet called ZipCodeDB that looks like the one below so the formulas had access to meaningful data for testing purposes).
MrExcel_20240131_C (version 1).xlsx
BGH
1ZIPLatLong
20280941.6825-71.2676
30281641.6914-71.5768
40286141.8814-71.356
50648241.4087-73.2485
61001740.7517-73.9707
71174740.7946-73.403
81944040.2778-75.2975
93502233.3224-86.9657
107310835.4445-97.5619
118710935.1506-106.569
129071233.8512-118.1457
139100134.1912-118.1392
149140234.2262-118.447
159171434.0197-117.9587
169193232.5783-117.1148
179236435.4667-115.2722
189260633.6951-117.8224
199269233.6144-117.6433
209281733.8512-117.7915
219450737.8537-122.0229
ZipCodeDB

Lastly, I wanted to explain how you can use the formula I offered to dig in further to understand what some of the intermediate results look like. Below, I've dragged the formula (that spills results in the Planning table) to a cell below the table to give more room for spilling results both down and to the right. Then I copied the interior of the last results formula (res) and replaced the final instruction to display "res" with that inner formula. This is the inner array of results that are processed by the "res" formula to eliminate duplicates, sort the results, and take the shortest 5 distances. You'll see there are a number of blanks (1st column) for distance because my version of ZipCodeDB did not contain ZIP codes for those team members, otherwise, the output follows the order of your TEAM worksheet. A more complete ZIPCodeDB that includes those members would be useful, or alternatively, trim down the list of team members to include only those whose ZIP code is present in the ZIPCode DB worksheet (this might be better to keep the example small). In any case, after interrogating a formula using this type of approach, you can either edit the formula to replace the last instruction with "res" and return it to its place in your worksheet, or delete the formula altogether and copy/paste from a neighboring row in the results table. In the snippet below, the bottom of the example Planning table and then below it, the formula interrogation just described. I mention this because when explaining what you want to do, it might be useful to present an example of what the code should do, and ultimately it would be an array resembling what is shown below, before extracting the five shortest distances (so this gives you the ability to copy meaningful intermediate results for posting):
MrExcel_20240131_C (version 1).xlsx
IJKLM
15Customer1850VINITAJONES92692
16Customer1031VINITAJONES92692
17Customer1295LARRYSMITH19440
18
19 STEVENJONES85283
20ROBERTSMITH85085
213999THOMASAPPLE91402
223960VINITAJONES92692
234146SHANESMITH94507
24THERESAJONES80015
25CASEYSMITH80550
26210BOBSMITH2861
2770THOMASSMITH6482
28204DAVIDSMITH2809
29BILLSMITH33019
30JOBSMITH46311
31BYRONSMITH60563
32STEVEJONES60610
33JOBSMITH46311
34MARCUSSMITH41091
35210BOBSMITH2861
36204DAVIDSMITH2809
37210BOBSMITH2861
38JOELSMITH48323
39JOBSMITH46311
40LOUIESMITH64118
41JAMESSMITH28031
42210BOBSMITH2861
43170LARRYSMITH19440
44JEFFREYSMITH89431
4548ANDREWSMITH10017
4670THOMASSMITH6482
47170LARRYSMITH19440
48204DAVIDSMITH2809
49210BOBSMITH2861
50ANDRESMITH45420
51JOELSMITH48323
52170LARRYSMITH19440
53DAVIDSMITH19006
54ERICSMITH19341
55BRIANSMITH19341
56210BOBSMITH2861
57204DAVIDSMITH2809
58BRANDONSMITH77382
59000
60000
Planning
Cell Formulas
RangeFormula
I15:I17I15=IF($G15<$AF$1,$L15,$AH$1)
J15:AC17J15=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,F15, 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 )
J19:M97J19=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,F2, 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))), HSTACK(dist,fname,lname,zip) )
Dynamic array formulas.
 
Upvote 0
I would recommend beginning a new post and mention in it that you previously worked through the forum to develop a formula-based solution to this problem, but now you'd like some help achieving a similar output using VBA...and mention VBA in the post title, and link to this thread in the body of your message (click on the line-node icon in the upper right of the most appropriate post number to get the URL for it). The reasons?...a new post is more likely to draw fresh eyes upon it, and by clarifying that you want help with VBA, you'll have a better chance getting expertise in that area. Additionally, your original post included a lot of formulas and a Formulas worksheet that have been eliminated, and the emphasis on looking at States has been resolved...both of which could cause confusion if someone tries to work through this thread.

So I think a fresh post would be appropriate, but be sure to include the necessary minisheets to create a working example. In addition to posting a small example of the Planning worksheet displaying the desired results, also include small examples of the TEAM worksheet and ZipCodeDB (I needed to construct a worksheet called ZipCodeDB that looks like the one below so the formulas had access to meaningful data for testing purposes).
MrExcel_20240131_C (version 1).xlsx
BGH
1ZIPLatLong
20280941.6825-71.2676
30281641.6914-71.5768
40286141.8814-71.356
50648241.4087-73.2485
61001740.7517-73.9707
71174740.7946-73.403
81944040.2778-75.2975
93502233.3224-86.9657
107310835.4445-97.5619
118710935.1506-106.569
129071233.8512-118.1457
139100134.1912-118.1392
149140234.2262-118.447
159171434.0197-117.9587
169193232.5783-117.1148
179236435.4667-115.2722
189260633.6951-117.8224
199269233.6144-117.6433
209281733.8512-117.7915
219450737.8537-122.0229
ZipCodeDB

Lastly, I wanted to explain how you can use the formula I offered to dig in further to understand what some of the intermediate results look like. Below, I've dragged the formula (that spills results in the Planning table) to a cell below the table to give more room for spilling results both down and to the right. Then I copied the interior of the last results formula (res) and replaced the final instruction to display "res" with that inner formula. This is the inner array of results that are processed by the "res" formula to eliminate duplicates, sort the results, and take the shortest 5 distances. You'll see there are a number of blanks (1st column) for distance because my version of ZipCodeDB did not contain ZIP codes for those team members, otherwise, the output follows the order of your TEAM worksheet. A more complete ZIPCodeDB that includes those members would be useful, or alternatively, trim down the list of team members to include only those whose ZIP code is present in the ZIPCode DB worksheet (this might be better to keep the example small). In any case, after interrogating a formula using this type of approach, you can either edit the formula to replace the last instruction with "res" and return it to its place in your worksheet, or delete the formula altogether and copy/paste from a neighboring row in the results table. In the snippet below, the bottom of the example Planning table and then below it, the formula interrogation just described. I mention this because when explaining what you want to do, it might be useful to present an example of what the code should do, and ultimately it would be an array resembling what is shown below, before extracting the five shortest distances (so this gives you the ability to copy meaningful intermediate results for posting):
MrExcel_20240131_C (version 1).xlsx
IJKLM
15Customer1850VINITAJONES92692
16Customer1031VINITAJONES92692
17Customer1295LARRYSMITH19440
18
19 STEVENJONES85283
20ROBERTSMITH85085
213999THOMASAPPLE91402
223960VINITAJONES92692
234146SHANESMITH94507
24THERESAJONES80015
25CASEYSMITH80550
26210BOBSMITH2861
2770THOMASSMITH6482
28204DAVIDSMITH2809
29BILLSMITH33019
30JOBSMITH46311
31BYRONSMITH60563
32STEVEJONES60610
33JOBSMITH46311
34MARCUSSMITH41091
35210BOBSMITH2861
36204DAVIDSMITH2809
37210BOBSMITH2861
38JOELSMITH48323
39JOBSMITH46311
40LOUIESMITH64118
41JAMESSMITH28031
42210BOBSMITH2861
43170LARRYSMITH19440
44JEFFREYSMITH89431
4548ANDREWSMITH10017
4670THOMASSMITH6482
47170LARRYSMITH19440
48204DAVIDSMITH2809
49210BOBSMITH2861
50ANDRESMITH45420
51JOELSMITH48323
52170LARRYSMITH19440
53DAVIDSMITH19006
54ERICSMITH19341
55BRIANSMITH19341
56210BOBSMITH2861
57204DAVIDSMITH2809
58BRANDONSMITH77382
59000
60000
Planning
Cell Formulas
RangeFormula
I15:I17I15=IF($G15<$AF$1,$L15,$AH$1)
J15:AC17J15=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,F15, 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 )
J19:M97J19=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,F2, 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))), HSTACK(dist,fname,lname,zip) )
Dynamic array formulas.
Thank you very much. I will be sure to be more clearer in what I want to do and what I am expecting. I will take my time before posting it to be sure it is clear and concise.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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