Find closest to Target with a variable

GamerNeelie

New Member
Joined
May 21, 2022
Messages
25
Office Version
  1. 2013
Platform
  1. Windows
I have a complicated formula that I need to expand on to filter out any results with with less then a set figure

Below is a cut down example of a larger sheet

Table in (E-G)
I know how to sort the files closest to the target (C2) and return the match for name and times

Table in (I-K)
I want to be able to sort the list while hitting the variable in (F2) and display results in J6 with anything less then then the value in (F2) being cut out

Current code I am using to sort the list starting in Cell (F6) - I use CTRL+SHIFT when entering this to make it work
=INDEX($B$6:$B$14, MATCH(SMALL(ABS(B$6:$B$14-$C$2), ROW(F1)), IF(COUNTIF($F$4:F4, B$6:$B$14)<COUNTIF(B$6:$B$14, B$6:$B$14), ABS(B$6:$B$14-$C$2), "A"), 0))

Example.png


Hope I have explained it as best as I can.

First time on forum too.. So Hi :D

Thanks.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Also is there a way to stop duplicates if the values are exactly the same? EG John and Lisa

(Chances of two values being the same are very very small but just in case this does come up)
 

Attachments

  • Example.png
    Example.png
    17.5 KB · Views: 3
Upvote 0
Welcome to the MrExcel forum!

Consider:

Book1 (version 1).xlsb
ABCDEFGHIJKLMNO
1
2Target160Files4
3
4Raw DataSorted without filesSorted with Min filesUsing array functions
5NameResultFilesNameResultTimesNameResultTimesNameResultTimes
6Mark158.28Paul158.46Paul158.46Paul158.46
7Paul158.46Mark158.28Mark158.28Mark158.28
8John165.82Fred156.29Fred156.29Fred156.29
9Luke169.49John165.82Lisa165.88Lisa165.88
10Mike178.212Lisa165.88Jeff152.56Jeff152.56
11Lisa165.88Jeff152.56Luke169.49Luke169.49
12Katie175.93Luke169.49Mike178.212Mike178.212
13Fred156.29Katie175.93   
14Jeff152.56Mike178.212   
15
Sheet16
Cell Formulas
RangeFormula
E6:E14E6=INDEX($A:$A,AGGREGATE(15,6,ROW($A$6:$A$14)/($B$6:$B$14=F6),COUNTIF($F$6:$F6,$F6)))
F6:F14F6=INDEX($B$6:$B$14, MATCH(SMALL(ABS(B$6:$B$14-$C$2), ROW(F1)), IF(COUNTIF($F$4:F4, B$6:$B$14)<COUNTIF(B$6:$B$14, B$6:$B$14), ABS(B$6:$B$14-$C$2), "A"), 0))
G6:G14G6=INDEX($C:$C,AGGREGATE(15,6,ROW($A$6:$A$14)/($B$6:$B$14=F6),COUNTIF($F$6:$F6,$F6)))
I6:I14I6=IF(J6="","",INDEX($A:$A,AGGREGATE(15,6,ROW($A$6:$A$14)/($B$6:$B$14=J6)/($C$6:$C$14>=$F$2),COUNTIF($J$6:$J6,$J6))))
J6:J14J6=IFERROR(INDEX($B$6:$B$14,MATCH(AGGREGATE(15,6,ABS($B$6:$B$14-$C$2)/($C$6:$C$14>=$F$2),ROWS($J$6:$J6)),IF($C$6:$C$14<$F$2,-1,ABS($B$6:$B$14-$C$2)),0)),"")
K6:K14K6=IF(J6="","",INDEX($C:$C,AGGREGATE(15,6,ROW($A$6:$A$14)/($B$6:$B$14=J6)/($C$6:$C$14>=$F$2),COUNTIF($J$6:$J6,$J6))))
M6:O12M6=LET(t,SORTBY(A6:C14,ABS(B6:B14-C2)),FILTER(t,INDEX(t,0,3)>=F2))
Press CTRL+SHIFT+ENTER to enter array formulas.
Dynamic array formulas.


The E and G formulas you can use with your existing formula to prevent the same name showing up when there's a tie. The I, J, K formulas should work for you to exclude the rows with not enough files.

And just to show how much easier it is with the new(ish) array formulas, how to do it with those in the M6 formula. Hope this helps!
 
Upvote 0
Solution
What do the 15,6 numbers means in the part of the forumula "AGGREGATE(15,6,ROW"

Ill be using this in different cells and sheets around my workbook but unsure what they are for?
 
Upvote 0
AGGREGATE is a composite function that does several functions. 15 is the identifier for SMALL. The next parameter is a modifier of the function. 6 means "Ignore errors." So the formula basically works by creating a list of numbers, dividing them by a TRUE/FALSE condition, and if it's false, it ends up dividing by 0 causing an error, thereby removing it from consideration.
 
Upvote 0
AGGREGATE is a composite function that does several functions. 15 is the identifier for SMALL. The next parameter is a modifier of the function. 6 means "Ignore errors." So the formula basically works by creating a list of numbers, dividing them by a TRUE/FALSE condition, and if it's false, it ends up dividing by 0 causing an error, thereby removing it from consideration.
Thank you so much for your help. Worked perfectly
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,597
Members
449,038
Latest member
Arbind kumar

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