+ Reply to Thread
Results 1 to 20 of 20

Returning an array of lookup values from an input array, rather than single value

Hybrid View

  1. #1
    Registered User
    Join Date
    05-03-2012
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2003, 2010
    Posts
    14

    Returning an array of lookup values from an input array, rather than single value

    Hi there,

    I have a range of values that will be a non-contiguous subset of the header row of a table of data. I need to retrieve an array of data a known offset from the cells that the range of values match.

    I thought the obvious approach was to use HLOOKUP and pass the range in, expecting to have an array of reults returned (CSE):

    {=HLOOKUP(Lookup_Values, Table_of_Data, Offset, 0)}

    This fails as HLOOKUP will only return the first result, not the complete array. The same happens with VLOOKUP.

    My next attempt was to use MATCH, which successfully returns an array of integers, representing the columns that hold the matched headers:

    {=MATCH(Lookup_Values, Table_of_Data,0)} = Array_of_columns, eg {2,4,5,7}.

    From there I thought it would be trivial to build an array of addresses that I could resolve with INDIRECT:

    Address_Array = {"Somesheet!B15", "Somesheet!D15", "Somesheet!E15", "Somesheet!G1"}

    {INDIRECT(Address_Array)}

    This also fails as INDIRECT only returns the first result.

    Any advice on how to solve this, or suggestions for a different approach would be appreciated.

    Cheers, Jason.
    Attached Files Attached Files
    Last edited by jase250; 05-08-2012 at 07:54 AM. Reason: Spelling corrections

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Returning an array of lookup values from an input array, rather than single value

    =INDEX(some_data,MATCH(Lookup_Values, Table_of_Data,0))
    array entered should do it I reckon. a sample workbook would have made it clearer though so I could be wrong.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    05-03-2012
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2003, 2010
    Posts
    14

    Re: Returning an array of lookup values from an input array, rather than single value

    Thanks JosephP - neglected to mention that I had also tried INDEX, and this only reurns a single value as well (based on the first member of the array), rather than an array.

    I will edit my post and submit a sample.

    Cheers, Jason.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Returning an array of lookup values from an input array, rather than single value

    index should work unless one of us missed something in your post :-)

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

    Re: Returning an array of lookup values from an input array, rather than single value

    Possibly a moot point if SUMIF is OK for you but actually I think that OFFSET formula was unnecessarily complex, this one should work

    =OFFSET(K13:T13,0,MATCH(Lookup_Items,Lookup_Row,0)-1,1,1)

    .......but it actually returns an array of references, so, as you say, if you wrap that with a COUNTA function you'll get 1......you need another step to "de-reference" the references, for numeric data you can use N function [for text it would be T] so that becomes

    =N(OFFSET(K13:T13,0,MATCH(Lookup_Items,Lookup_Row,0)-1,1,1))

    If you wrap that in COUNTA you should get 4 as expected (with numeric values in the table)
    Audere est facere

  6. #6
    Registered User
    Join Date
    05-03-2012
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2003, 2010
    Posts
    14

    Re: Returning an array of lookup values from an input array, rather than single value

    Thanks again, daddylonglegs.

    I will tuck this technique away for future use, but I like the elegance of the SUMIF function. I have to convert some graphics to text on the fly already to return a table I can work with - no issue to have it converted to a boolean table instead.

    Cheers, Jason.

  7. #7
    Registered User
    Join Date
    05-03-2012
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2003, 2010
    Posts
    14

    Re: Returning an array of lookup values from an input array, rather than single value

    Have uploaded a representation of my problem, which includes my attempt to use INDEX and the result from it.

    Appreciate the time you have taken,

    Cheers, Jason.

  8. #8
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Returning an array of lookup values from an input array, rather than single value

    LOOKUP allows you to input a range as the lookup value and return an array, e.g.

    =LOOKUP(Lookup_Items,Lookup_Row,K13:T13)

    .....but for LOOKUP to work Lookup_Row must be sorted ascending.

    ...alternatively for returning numeric results try SUMIF

    =SUMIF(Lookup_Row,Lookup_Items,K13:T13)

    No sorting required

    Note: for either formula you can also derive K13:T13 using INDEX/MATCH based on B3 if you want e.g. something like =INDEX(K4:T18,MATCH(B3,J4:J18,0),0)

  9. #9
    Registered User
    Join Date
    05-03-2012
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2003, 2010
    Posts
    14

    Re: Returning an array of lookup values from an input array, rather than single value

    Thanks daddylonglegs.

    Had experimented with lookup, but as you say: the data must be sorted and I cannot rely on other departments to adhere to this.

    The lookup table is essentially a truth table that I need the array of values to be returned from - summing them is not useful, unfortunately - unless I misunderstand the functionality of SUMIF. (Summing a selection of data based on criteria being met...?)

    Cheers, Jason.

  10. #10
    Forum Expert icestationzbra's Avatar
    Join Date
    01-07-2004
    MS-Off Ver
    2007, 2010
    Posts
    1,421

    Re: Returning an array of lookup values from an input array, rather than single value

    if i understand your requirement correctly (and if i do not, my apologies):

    if you weren't able to get a proper result out of the INDEX function, it is due to the fact that your named_ranges were slightly askew. in lookup_table, cell J3 was included, but in lookup_row, it was not.

    see attached:
    Attached Files Attached Files
    - i.s.z -
    CSE, aka Array aka { }, formulae are confirmed with CONTROL+SHIFT+ENTER.
    Replace commas ( , ) with semicolons ( ; ) in formulae, if your locale setting demands.
    All good ideas are courtesy resources from this forum as well as others around the web.
    - e.o.m -

  11. #11
    Forum Expert icestationzbra's Avatar
    Join Date
    01-07-2004
    MS-Off Ver
    2007, 2010
    Posts
    1,421

    Re: Returning an array of lookup values from an input array, rather than single value

    and, in continuation, when you talk of getting the full array ({2,4,6,7}) in the results, do you mean that you would like to get the results corresponding to Value_10 for Item2, Item4, Item6 and Item7? if yes, then you would have to use SMALL function to induce the INDEX function to give you one result at a time over adjacent cells upon the formula being dragged over a few (adjacent) columns (or rows)... it is not possible to get all those values within the same cell.

  12. #12
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Returning an array of lookup values from an input array, rather than single value

    My 2 cents added:
    Attached Files Attached Files
    Last edited by WHER; 05-08-2012 at 08:35 AM.

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Returning an array of lookup values from an input array, rather than single value

    you can't return an array (visibly) to one cell so what is it you actually want to do with it? I reckon daddylonglegs' answer is probably what you want.

  14. #14
    Registered User
    Join Date
    05-03-2012
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2003, 2010
    Posts
    14

    Re: Returning an array of lookup values from an input array, rather than single value

    Thanks again JosephP and icestationzbra:

    I don't need to see the array at this stage - this is an intermediate step. Once it has been returned a number of other functions are performed and MIN applied to give the final result visible in the cell - this is part of an optimisation routine which attempts to minimise costs.

    Cheers, Jason.

  15. #15
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Returning an array of lookup values from an input array, rather than single value

    This formula

    =SUMIF(Lookup_Row,Lookup_Items,K13:T13)

    returns this array {1;1;0;0}

    ......so it will give the required result, I think, as long as you don't have duplicates in Lookup_Row......and as long as the results to be returned are numeric

  16. #16
    Registered User
    Join Date
    05-03-2012
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2003, 2010
    Posts
    14

    Re: Returning an array of lookup values from an input array, rather than single value

    Thanks daddylonglegs: I had dismissed the option of SUMIF early on as it didn't seem to have the potential to return arrays of data. While the table I have is actually text based, it will be no issue to convert it to boolean values.

    This is exactly what I needed - the resultant array stored internally so it can be operated on.

    Aprreciate all the contributions that were actually displaying the arrays (JosephP, WHER, icestationzbra). Due to the inputs being dynamic ranges this solution is not feasible. Apologies for not being clear about this at the beginning.

    Thanks again folks.

    Cheers, Jason.

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Returning an array of lookup values from an input array, rather than single value

    basically select 4 cells across, type
    =INDEX($K$4:$T$18,MATCH(B3,$J$4:$J$18,0),MATCH($A$3:$A$6,$K$3:$T$3,0))
    and Ctrl+shift+enter
    in your sample you used value_10 as the lookup rather than value10 though. ;-)

  18. #18
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Returning an array of lookup values from an input array, rather than single value

    I reckon it would help to know the final required outcome instead then. or you can just array-enter the index formula into the 4 cells and refer to that range in your other functions (easier to audit too)

  19. #19
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Returning an array of lookup values from an input array, rather than single value

    Did you try SUMIF? If not then this formula would also work, even for non-numeric values

    =OFFSET(K13:T13,0,SMALL(MATCH(Lookup_Items,Lookup_Row,0)-1,ROW(INDIRECT("1:"&COUNT(MATCH(Lookup_Items,Lookup_Row,0))))),1,1)

  20. #20
    Registered User
    Join Date
    05-03-2012
    Location
    Christchurch, New Zealand
    MS-Off Ver
    Excel 2003, 2010
    Posts
    14

    Re: Returning an array of lookup values from an input array, rather than single value

    Tried the offset one, but seems to only be evlauting the first value of the lookup_items array. Wrapping a COUNTA around it returns 1, and trying to reference it with index gives #REF when I put it in my sample sheet.

    I had experimented with OFFSET early on in the piece, but always ended up with a single value returned.

+ 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