+ Reply to Thread
Results 1 to 7 of 7

Double Criteria Search - Exact Match on First but Closest Match on Second

  1. #1
    Registered User
    Join Date
    01-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    6

    Double Criteria Search - Exact Match on First but Closest Match on Second

    Have searched the boards and can't find this exact scenario, although odd, I imagine its quite common.

    Example:

    Item Cost Return Value
    Car 100 A
    Car 200 B
    Car 300 C
    Bus 100 D
    Bus 200 E
    Bus 300 F
    Train 100 G
    Train 200 H
    Train 300 I

    I need to search the table above to match two items: Item and Cost to return the 3rd column.
    Column1 needs to be an exact match, but column2 needs to be a closest match that is less than or equal to the value requested.

    e.g.
    Car 350 should return C
    Train 133 should return G

    I've only seen solutions to the effect of an array formula index(match( concatenating the two ranges and then using a closest match but that doesn't work as this isn't an ordered list by any means.

    Please, does anyone have any suggestions? I've been stuck for hours.... Thank you in advance!
    Double Example.xls

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Double Criteria Search - Exact Match on First but Closest Match on Second

    Both of these formulas requires the data to be grouped together in column A and sorted ascending in column B. This first will work with your data as presented:

    G2: =INDEX(OFFSET(INDEX(C:C, MATCH(E2,A:A,0)), , , COUNTIF(A:A, E2), ), MATCH(F2, OFFSET(INDEX(B:B, MATCH(E2, A:A, 0)), , , COUNTIF(A:A, E2), )), 1)


    This next one requires you to sort the table ascending by column A, then by column B, thus putting the BUS rows at the top:

    G2: =INDEX($C$2:$C$10, MATCH(E2&"-"&F2, INDEX($A$2:$A$10&"-"&$B$2:$B$10, 0), 1))
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    01-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Double Criteria Search - Exact Match on First but Closest Match on Second

    Thanks so much Jerry! Worked like a charm. I see now that the sorting is key, duh! You've saved me! Thanks again.

  4. #4
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Double Criteria Search - Exact Match on First but Closest Match on Second

    If the data is sorted on A and B, then
    =LOOKUP(2,1/((A2:A10=E2)*(B2:B10<=F2)),C2:C10)

    By the way, JBeaucaire's second formula will only work properly if all the numbers have the same number of digits (since it is treating them as text).
    Good luck.

  5. #5
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Double Criteria Search - Exact Match on First but Closest Match on Second

    This formula will work with your data in any order

    =INDEX(C2:C100,MATCH(1,(B2:B100=MAX(IF(A2:A100=E2,IF(B2:B100<=F2,B2:B100))))*(A2:A100=E2),0))

    confirmed with CTRL+SHIFT+ENTER
    Audere est facere

  6. #6
    Registered User
    Join Date
    01-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Double Criteria Search - Exact Match on First but Closest Match on Second

    Quote Originally Posted by OnErrorGoto0 View Post
    If the data is sorted on A and B, then
    =LOOKUP(2,1/((A2:A10=E2)*(B2:B10<=F2)),C2:C10)

    By the way, JBeaucaire's second formula will only work properly if all the numbers have the same number of digits (since it is treating them as text).


    Thanks for pointing out the text issue, as I did run into that problem once I increased the number of digits. What a great forum full of brilliant people!

    I've checked the forums and have just one last question. Would appreciate if someone could please recommend any books or articles on advanced Excel array formulas (not VBA).

  7. #7
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Double Criteria Search - Exact Match on First but Closest Match on Second

    I believe that Bob Umlas has a new book out which almost certainly involves weird array formulas.
    You could also I dare say spend a few profitable days reading through the past posts of Messrs. DaddyLongLegs, DonkeyOte and NBVC (among others).

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1