Finding Nearest 3 Values Whether Higher Or Lower

Pejayuk

New Member
Joined
Aug 19, 2004
Messages
39
Hi, I would really appreciate help with finding a solution for the following.

I have a list of data contained in columns A-K. This data cannot be sorted and I need to find the 3 nearest values to the value I select from column F.

I've been trying to achieve this by using {=MIN(IF(O3:O30>L2,O3:O30))} and {=MAX(IF(O3:O30<L2,O3:O30))}. (I'm only using 30 rows in this test). If I repeat these formulas a few times, it does give roughly the correct results, but also some I don't want.

It doesn't work at all if I select the lowest or highest value. For example: If I test it with values 10 - 300 increasing by 10 per row. If I select 10, because there is no lower value, I would like it to find the three closest higher values. Likewise, if I select 300, I would like to display 290, 280, 270 in that order. Another problem is, the actual data in column F will have a minimal number of duplicates.

Thanks in advance for any suggestions.
 
Try this Array formula:
Book1
AFLM
1ValuesCriteriaResults
2154
346
463
587
632
75
82
97
105
Hoja1
Cell Formulas
RangeFormula
M2:M6M2{=INDEX($F$2:$F$10,MATCH(SMALL(IF($F$2:$F$10<>$L$2,ABS(($F$2:$F$10+(ROW($F$2:$F$10)/100))-$L$2)),ROWS($C$2:C2)),ABS(($F$2:$F$10+(ROW($F$2:$F$10)/100))-$L$2),0))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Last edited:
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Please try at B2
Book1
AB
140
22839
33343
43933
54348
64828
753
858
963
1068
1173
Sheet1
Cell Formulas
RangeFormula
B2:B6B2=INDEX(A:A,MOD(SMALL(INDEX(ABS($A$2:$A$11-$B$1)*10^9+ROW($A$2:$A$11),),ROWS(B$2:B2)),100))
 
Upvote 0
Nice try, Bo_Ry, but you missed one of the requirements.

If the criteria value appears in the list, it should not be included in the results.

Also, it appears that neither your suggestion, or Dante's include any kind of limiter to blank out values which don't meet the criteria of '3 closest' (note in my last suggestion that 3 values are tied for 3rd place).
 
Upvote 0
Jason, I see.
How about this?

Book1
AB
140
24043
35543
44444
54345
643 
745 
858 
963 
101
111
121
13
14
15
16
Sheet1
Cell Formulas
RangeFormula
B2:B9B2{=IFERROR(SMALL(IF(($A$2:$A$18<>$B$1)*(ABS($A$2:$A$18-$B$1)<=AGGREGATE(15,6,ABS($A$2:$A$18-$B$1)/(FREQUENCY($A$2:$A$18,$A$2:$A$18)>0)/($A$2:$A$18<>$B$1),3)),$A$2:$A$18),ROWS(B$2:B2)),"")}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
That looks good for when the OP upgrades their version of excel, Bo (y)

I've discovered the version of Excel on there doesn't have the AGGREGATE or IFERROR functions.

Your formula is more concise and efficient than my attempt for newer versions of excel.
My last formula works with pre 2007 versions, however one thing that is still unclear (partially my fault for not asking the question) is the expected order of the results.

Testing with the data set that I used in post #9, your formula returns 81,95,95,97,97 where mine returns 97,97,95,95,81
In my opinion, the 'correct' result would be either 95,95,81,97,97 or 95,95,97,97,81 (95 is the closest value to the criteria of 89 so should be first). Although I think maybe it would be best to wait for some feedback from the OP before making any more changes to the formulas.
 
Upvote 0
Thanks, Jason.
Without IFERROR, my formula would be very long.
 
Upvote 0
Also, without AGGREGATE, your method might not be possible.

I had many attempts before finding one that works.
 
Upvote 0
Many thanks to all who have made suggestions. As Jason has said, the issue is needing to use this on an old version of Excel.

Jason, I have been testing your new formula and this is what I have found........

Using your sample list and target, I get the exact same 5 results. (97,97,95,95,81).
The only glitch seems to be when using a low number for the target.

Using my simple 10 -100 in increments of 10 sample. This is what I get.
Target number..............Result
1,2,3,4.............................0,0,0,0,0,0

5,6,7,8,9.........................10,0,0,0,0,0

10...................................20,0,0,0,0,0

11,12,13,14....................10,20,0,0,0,0

15...................................10,20,30,0,0,0

In this example, anything 15 and above seems to give perfect results. I don't get any error message, so I'm not sure if it is anything to do with the maximum allowable number of functions. I have also tested it on a 2016 version with the same results.

With regards to the expected order of results. Yes, the best outcome would be as you assumed, the nearest to the target first. Though I understand from what you said yesterday, that this won't be possible with the old Excel. The only time this would be an issue, is if more results were returned than I actually needed. (Which I know will happen in some circumstances, but it should still be fine).

Thanks again
 
Upvote 0
Using my simple 10 -100 in increments of 10 sample. This is what I get.
Target number..............Result
1,2,3,4.............................0,0,0,0,0,0
5,6,7,8,9.........................10,0,0,0,0,0
10...................................20,0,0,0,0,0
11,12,13,14....................10,20,0,0,0,0
15...................................10,20,30,0,0,0

Hi @Pejayuk, Did You try my formula from post #11?, it works with all your examples.

If you want 3 values, just copy the formula into 3 cells.
The following are examples of 6 nearby values, but if you only want 3, then put the formula in 3 cells.

Book1
AFLM
1ValuesCriteriaResults
210110
32020
43030
54040
65050
76060
870
980
1090
11100
Sheet
Cell Formulas
RangeFormula
M2:M7M2{=INDEX($F$2:$F$11,MATCH(SMALL(IF($F$2:$F$11<>$L$2,ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2)),ROWS($C$2:C2)),ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2),0))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.



Book1
AFLM
1ValuesCriteriaResults
210410
32020
43030
54040
65050
76060
870
980
1090
11100
Sheet
Cell Formulas
RangeFormula
M2:M7M2{=INDEX($F$2:$F$11,MATCH(SMALL(IF($F$2:$F$11<>$L$2,ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2)),ROWS($C$2:C2)),ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2),0))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.


Book1
AFLM
1ValuesCriteriaResults
210510
32020
43030
54040
65050
76060
870
980
1090
11100
Sheet
Cell Formulas
RangeFormula
M2:M7M2{=INDEX($F$2:$F$11,MATCH(SMALL(IF($F$2:$F$11<>$L$2,ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2)),ROWS($C$2:C2)),ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2),0))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.


Book1
AFLM
1ValuesCriteriaResults
2101020
32030
43040
54050
65060
76070
870
980
1090
11100
Sheet
Cell Formulas
RangeFormula
M2:M7M2{=INDEX($F$2:$F$11,MATCH(SMALL(IF($F$2:$F$11<>$L$2,ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2)),ROWS($C$2:C2)),ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2),0))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.


Book1
AFLM
1ValuesCriteriaResults
2101510
32020
43030
54040
65050
76060
870
980
1090
11100
Sheet
Cell Formulas
RangeFormula
M2:M7M2{=INDEX($F$2:$F$11,MATCH(SMALL(IF($F$2:$F$11<>$L$2,ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2)),ROWS($C$2:C2)),ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2),0))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.


Book1
AFLM
1ValuesCriteriaResults
2102010
32030
43040
54050
65060
76070
870
980
1090
11100
Sheet
Cell Formulas
RangeFormula
M2:M7M2{=INDEX($F$2:$F$11,MATCH(SMALL(IF($F$2:$F$11<>$L$2,ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2)),ROWS($C$2:C2)),ABS(($F$2:$F$11+(ROW($F$2:$F$11)/1000))-$L$2),0))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
Book1
KLMNO
1Errror when more than 3No ErrorNo Limit
2Criteria10List
3Results20202010
420202020
530303030
630303040
7#NUM! 4050
8#NUM! 4060
9#NUM! 5070
10#NUM! 5080
11#NUM! 6090
12#NUM! 60100
1310
1420
1530
1640
1750
1860
1970
2080
2190
22100
23
24
25
26
27
28
29
Sheet2
Cell Formulas
RangeFormula
L3:L12L3{=MOD(SMALL(IF(($O$3:$O$30<>"")*(ABS($O$3:$O$30-$L$2)<SMALL(IF($O$3:$O$30<>"",IF(FREQUENCY($O$3:$O$30,$O$3:$O$30),ABS($O$3:$O$30-$L$2))),4))*($O$3:$O$30<>$L$2),ABS($O$3:$O$30-$L$2)*10^9+$O$3:$O$30),ROWS(L$3:L3)),10^6)}
M3:M12M3{=IF(ROWS(M$3:M3)>SUM(($O$3:$O$30<>"")*(ABS($O$3:$O$30-$L$2)<SMALL(IF($O$3:$O$30<>"",IF(FREQUENCY($O$3:$O$30,$O$3:$O$30),ABS($O$3:$O$30-$L$2))),4))*($O$3:$O$30<>$L$2)),"",MOD(SMALL(IF($O$3:$O$30*(ABS($O$3:$O$30-$L$2)<SMALL(IF($O$3:$O$30<>"",IF(FREQUENCY($O$3:$O$30,$O$3:$O$30),ABS($O$3:$O$30-$L$2))),4))*($O$3:$O$30<>$L$2),ABS($O$3:$O$30-$L$2)*10^9+$O$3:$O$30),ROWS(M$3:M3)),10^6))}
N3:N12N3{=MOD(SMALL(IF(($O$3:$O$30<>"")*($O$3:$O$30<>$L$2),ABS($O$3:$O$30-$L$2)*10^9+$O$3:$O$30),ROWS(N$3:N3)),10^6)}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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