# Looking up minimum value position in a range subset

A      2
A      6
A      12
B      1
B      2
B      3

How could I write up an array function in Excel to return the position of the row in a subset A, where the second column is the closest to number 5?

I'm thinking of something along the lines of {=MATCH(1, IF(AND(A:A="A",B:B=MIN(IF(A:A="A",ABS(B:B-5),999))),1,0),0)}

Hope the above makes sense.

Thanks
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You Okay with a user defined function for the same??

Saurabh...
AnalystAuthor Commented:
I would much prefer an array function - a UDF would make sharing an issue, it also is less "tidy".
AnalystAuthor Commented:
Sorry, I should have added, the expected result is "2" - i.e. second row.
Microsoft Excel ExpertCommented:
you can simply do this with array formula =MATCH(MIN(IF(A1:A6="A",B1:B6)),B1:B6,0)

press with control shift enter.

see the attached example
Book1.xlsx

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Excel & VBA ExpertCommented:
Maybe this.....(Array Formula)

``````=MIN(IF(A2:A7="A",ABS(B2:B7-5)*(ROW(A2:A7)-ROW(A2)+1)))
``````

That will give you the row number of A subset where corresponding value in col. B is closest to 5.

Does this help?
Microsoft Excel ExpertCommented:
i did not see the criteria of closest to 5.

the formula from sktneer perfectly works for you
AnalystAuthor Commented:
AnalystAuthor Commented:
Sorry, sktneer, I was testing out the solution that ProfessorJimJam provided before I saw your posts. Both solutions would work, I suppose. Thanks both.
Excel & VBA ExpertCommented:
No problem.

But you wrote this.....

How could I write up an array function in Excel to return the position of the row in a subset A, where the second column is the closest to number 5?
What was that?

If that was your requirement, how could you get the correct output with the formula suggested by ProfessorJimJam?
AnalystAuthor Commented:
I tweaked the formula slightly, before seeing your solution:
=MATCH(MIN(IF(A1:A6="A",ABS(B1:B6-5))),ABS(B1:B6-5),0)

Since both your formulas work and were submitted before I hit "accept" I will request that the points are split.

Thanks again.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.