Finding closest location

karattop

New Member
Joined
Jun 28, 2010
Messages
38
I have a mileage matrix with all the locations across the top and down the side. On another sheet I have a stockout sheet, and I want to find the three nearest locations to the ones that are stocked out.

So, if I have a stockout (=IF(B3<0,.....) I want to go to the mileage matrix, find that location in the matrix (VLOOKUP or MATCH?), look down the row to find the lowest mileage (SMALL) and then at the top of that column is where my nearest location is (function??). In subsequent columns I want the second nearest location (SMALL(x,2)) and the third nearest location (SMALL(x,3)).

In parentheses I've indicated the formulas I've been playing with to try to afford this goal. I think this might be a little more complex than what I'm used to....

Maybe I am going about it in completely the wrong way. Any ideas?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I have a mileage matrix with all the locations across the top and down the side. On another sheet I have a stockout sheet, and I want to find the three nearest locations to the ones that are stocked out.

So, if I have a stockout (=IF(B3<0,.....) I want to go to the mileage matrix, find that location in the matrix (VLOOKUP or MATCH?), look down the row to find the lowest mileage (SMALL) and then at the top of that column is where my nearest location is (function??). In subsequent columns I want the second nearest location (SMALL(x,2)) and the third nearest location (SMALL(x,3)).

In parentheses I've indicated the formulas I've been playing with to try to afford this goal. I think this might be a little more complex than what I'm used to....

Maybe I am going about it in completely the wrong way. Any ideas?
Where is all the data we need to look for?
 
Upvote 0
On the "Distance" sheet is the Mileage matrix. Row 1 has all the locations, and they are all repeated in column A. At the intersections of each row,col is the miles between the two locations, just like what you often see on maps.

On the stockout sheet, I have the location in column A and column B tells me if the location is stocked out by giving a negative number. So, if b3 is negative, in column C I want to see where the closest location is from where I can pull stock. In column D I want the second-closest location and in column E I want the third-closest location.

Eventually I will add what the stock is in those three locations and format according to which location has enough stock to ship to the OOS (out-of-stock) location.
 
Upvote 0
On the "Distance" sheet is the Mileage matrix. Row 1 has all the locations, and they are all repeated in column A. At the intersections of each row,col is the miles between the two locations, just like what you often see on maps.

On the stockout sheet, I have the location in column A and column B tells me if the location is stocked out by giving a negative number. So, if b3 is negative, in column C I want to see where the closest location is from where I can pull stock. In column D I want the second-closest location and in column E I want the third-closest location.

Eventually I will add what the stock is in those three locations and format according to which location has enough stock to ship to the OOS (out-of-stock) location.
Try this...

Mileage matrix on the Distance sheet in the range A1:G10.

On your other sheet enter this formula in C3 and copy across to E3:

=IF($B3<0,INDEX(Distance!$B$1:$G$1,MATCH(SMALL(INDEX(Distance!$B$2:$G$10,MATCH($A3,Distance!$A$2:$A$10,0),0),COLUMNS($C3:C3)),INDEX(Distance!$B$2:$G$10,MATCH($A3,Distance!$A$2:$A$10,0),0),0)),"")
 
Upvote 0
Is this something that suits your need?

At this stage I have not included any stock figures, just closest locations.
Excel Workbook
ABCDEFG
1312345
2Location ALocation BLocation CLocation DLocation E
3Location A015172014
4Location B150122125
5Location C171202315
6Location D202123011
7Location E142515110
8
91Zero Count
10ClosestMileageNearest
11Location C112Location B
12215Location E
13317Location A
Sheet
#VALUE!
 
Upvote 0
Try this...

=IF($B3<0,INDEX(Distance!$B$1:$G$1,MATCH(SMALL(INDEX(Distance!$B$2:$G$10,MATCH($A3,Distance!$A$2:$A$10,0),0),COLUMNS($C3:C3)),INDEX(Distance!$B$2:$G$10,MATCH($A3,Distance!$A$2:$A$10,0),0),0)),"")


WOWZA! It works. It'll take me a little time to figure out how this works, but I'm gonna do it! I think I'll also try the other solution given by Number Kruncher just cuz.... I love to learn!
 
Upvote 0
WOWZA! It works. It'll take me a little time to figure out how this works, but I'm gonna do it! I think I'll also try the other solution given by Number Kruncher just cuz.... I love to learn!
Don't let the length fool you!

If you get stuck just let me know and I'll walk you through it. Once you figure it out you'll then think: "heck, that's really quite simple and it makes perfect sense!".
 
Upvote 0
Don't let the length fool you!

If you get stuck just let me know and I'll walk you through it. Once you figure it out you'll then think: "heck, that's really quite simple and it makes perfect sense!".

I get it! It did take me awhile to get used to thinking in relative positions. Ingenious how you don't literally find the column header, but you take advantage of the symmetry of the mileage matrix.

Thanks again!
 
Upvote 0
Number Kruncher: Is 'Locations' a name? I've tried to imagine what it is, but have too many distractions today so I thought I'd just ask.
 
Upvote 0
I get it! It did take me awhile to get used to thinking in relative positions. Ingenious how you don't literally find the column header, but you take advantage of the symmetry of the mileage matrix.

Thanks again!
You're welcome. Thanks for the feedback! :cool:
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,219
Members
452,895
Latest member
BILLING GUY

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