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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Saurabh Singh TeotiaCommented:
You Okay with a user defined function for the same??

saulius88AnalystAuthor Commented:
I would much prefer an array function - a UDF would make sharing an issue, it also is less "tidy".
saulius88AnalystAuthor Commented:
Sorry, I should have added, the expected result is "2" - i.e. second row.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

ProfessorJimJamMicrosoft 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

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Maybe this.....(Array Formula)


Open in new window

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

I think this is what you asked in your question.

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

the formula from sktneer perfectly works for you
saulius88AnalystAuthor Commented:
Really helpful, thank you.
saulius88AnalystAuthor 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.
Subodh Tiwari (Neeraj)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?
saulius88AnalystAuthor Commented:
I tweaked the formula slightly, before seeing your solution:

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.