+ Reply to Thread
Results 1 to 19 of 19

Pulling a value from a matrix

  1. #1
    Registered User
    Join Date
    08-17-2019
    Location
    Orlando, FL
    MS-Off Ver
    Office 2010
    Posts
    7

    Pulling a value from a matrix

    Given a spreadsheet that contains a number of rows with a category and a number, such as:

    category 1 20,000

    I want to run that against the below table:

    100 200 300 400 500 600
    category 1 6,250 15,000 30,000 60,000 90,000 175,000
    category 2 5,500 15,000 34,000 70,000 120,000 250,000
    category 3 1600 1,500 3,000 6,500 8,000 11,000
    category 4 12,000 35,000 65,000 100,000 150,000 215,000

    Now in this case I want to compare that number to the range of values in the table for category 1. So in this case it matched category 1, now it needs to cycle through those values and if less than the number higher than it, return the header number at the top of the table for than range it falls in. So in this case the number is less than 30,000 for that category, and so it would return the header of 200 in the table. Hopefully you see how that works. I am able to grind out a series of IF statements that logically works, but with my table size it far exceeds the 64 nested ifs limit. I've played around with MATCH and INDEX but can't figure out how to get it to properly act on the entire table.

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,984

    Re: Pulling a value from a matrix

    The forum has messed up your formatting.

    Will you please attach a SMALL sample Excel workbook (10-20 rows of data is usually enough)? However, please give us an indication of the approximate number of rows of data you want the solution to work with (100, 1000, 100,000 or whatever). Please don't attach a picture of an Excel sheet (no-one will want to re-type all your stuff before starting).

    1. It does NOT have to be your real sheet - mock up a SAMPLE if you need to. But not 1000's of rows!!! It makes manual checking so tedious. Whatever you do... make sure that all confidential information is removed first!!

    2. Make sure that your sample data are truly REPRESENTATIVE of your real data. The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    3. Make sure that your desired solution is also shown (mock up the results manually).

    4. Try to avoid using merged cells. They cause lots of problems!

    Unfortunately the attachment icon doesn't work at the moment. So, to attach an Excel file you have to do the following: Just before posting, scroll down to Go Advanced and then scroll down to Manage Attachments. Now follow the instructions at the top of that screen.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  3. #3
    Registered User
    Join Date
    08-17-2019
    Location
    Orlando, FL
    MS-Off Ver
    Office 2010
    Posts
    7

    Re: Pulling a value from a matrix

    I believe I've now attached a representative sample of the table. The file with the data could have up to 30K or so rows in it to be compared to the table as described in the original post.
    Attached Files Attached Files

  4. #4
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,984

    Re: Pulling a value from a matrix

    Hi. You can use this:

    =SMALL(INDEX($B$2:$G$5,MATCH(J2,$A$2:$A$5,0),),COUNTIF(INDEX($B$2:$G$5,MATCH(J2,$A$2:$A$5,0),),"<"&$K$2)+1)
    Attached Files Attached Files

  5. #5
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Pulling a value from a matrix

    Quote Originally Posted by Glenn Kennedy View Post
    The forum has messed up your formatting.
    I know the OP posted a file in response to your request, but a trick I discovered at the forum I mainly volunteer at (and which works here) is to click the "Reply With Quote" hyperlink... in a lot of cases (not all, but in a lot), this one included, you can see the intended layout displayed correctly (which you can then copy/paste into a worksheet).

  6. #6
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,984

    Re: Pulling a value from a matrix

    OMG! Of course I recognise your name from "another forum", as I occasionally lurk there. That is so useful... Do have some rep... even if you couldn't give a da##. You earned it!

    My problem will be trying to remember that tip tomorrow and the next day...

    Welcome back to the dystopia that is the Excel Forum... however briefly!

  7. #7
    Registered User
    Join Date
    08-17-2019
    Location
    Orlando, FL
    MS-Off Ver
    Office 2010
    Posts
    7

    Re: Pulling a value from a matrix

    So that almost got me there. The formula you gave and the sample spreadsheet is pointing to the correct value in the matrix per the number of the sample value. However, I actually want it to display the number in the topmost row of that column, which in the example case is 50.
    Attached Files Attached Files

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,410

    Re: Pulling a value from a matrix

    I think this is what you want:

    =INDEX($B$1:$G$1,,MATCH(K2,OFFSET($B$1:$G$1,MATCH(J2,$A$2:$A$5,0),),1))
    Last edited by AliGW; 08-18-2019 at 02:05 AM.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  9. #9
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,984

    Re: Pulling a value from a matrix

    Ahh yes! Silly me. I forgot about that bit. So, the use of the volatile OFFSET function can be avoided and this can be used instead:

    =INDEX($B$1:$G$1,,COUNTIF(INDEX($B$2:$G$5,MATCH(J2,$A$2:$A$5,0),),"<"&$K$2)+1)
    Last edited by Glenn Kennedy; 08-18-2019 at 04:27 AM.

  10. #10
    Forum Contributor
    Join Date
    10-30-2003
    Location
    Singapore
    MS-Off Ver
    Excel 2019
    Posts
    197

    Re: Pulling a value from a matrix

    Maybe,

    =LOOKUP(1,0/(INDEX(B2:G5,MATCH(J2,A2:A5,0),0)<=K2),B1:G1)

    Regards
    Bosco

  11. #11
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: Pulling a value from a matrix

    Pl see file
    ARRAY formula
    In L2
    =INDEX($B$1:$G$1,MATCH(FALSE,INDEX($B$2:$G$5,MATCH(J2,$A$2:$A$5,0),)<=K2,0)-1)

    OR

    In L4
    =SUMPRODUCT(MAX(((($B$2:$G$5)*($A$2:$A$5=J2))<=K2)*((($B$2:$G$5)*($A$2:$A$5=J2))>0)*$B$1:$G$1))
    Attached Files Attached Files
    Last edited by kvsrinivasamurthy; 08-18-2019 at 08:16 AM.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  12. #12
    Registered User
    Join Date
    08-17-2019
    Location
    Orlando, FL
    MS-Off Ver
    Office 2010
    Posts
    7

    Re: Pulling a value from a matrix

    Quote Originally Posted by Glenn Kennedy View Post
    Ahh yes! Silly me. I forgot about that bit. So, the use of the volatile OFFSET function can be avoided and this can be used instead:

    =INDEX($B$1:$G$1,,COUNTIF(INDEX($B$2:$G$5,MATCH(J2,$A$2:$A$5,0),),"<"&$K$2)+1)
    Awesome, thanks! We're 99% there. One final thing and I may figure out before you do- in cases where a value is < a value in column B, the formula throws a #VALUE. In cases where a value is less than the column B value, I would like it to just return the header from column B. The formula works fine for the other side of the matrix- cases where a value is greater than the column G value, it returns the header from column G.

  13. #13
    Registered User
    Join Date
    08-17-2019
    Location
    Orlando, FL
    MS-Off Ver
    Office 2010
    Posts
    7

    Re: Pulling a value from a matrix

    Actually I figured it out all, thanks for the help!!

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,410

    Re: Pulling a value from a matrix

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  15. #15
    Registered User
    Join Date
    08-17-2019
    Location
    Orlando, FL
    MS-Off Ver
    Office 2010
    Posts
    7

    Re: Pulling a value from a matrix

    Quote Originally Posted by kvsrinivasamurthy View Post
    Pl see file
    ARRAY formula
    In L2
    =INDEX($B$1:$G$1,MATCH(FALSE,INDEX($B$2:$G$5,MATCH(J2,$A$2:$A$5,0),)<=K2,0)-1)

    OR

    In L4
    =SUMPRODUCT(MAX(((($B$2:$G$5)*($A$2:$A$5=J2))<=K2)*((($B$2:$G$5)*($A$2:$A$5=J2))>0)*$B$1:$G$1))
    So for your first formula I modified it by increasing the ranges, as below:

    =INDEX($B$1:$G$1,MATCH(FALSE,INDEX($B$2:$G$29,MATCH(I2,$A$2:$A$29,0),)<=J2,0)-1)

    Now, I am getting a #VALUE! for all results even though they are valid values within the table.

  16. #16
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,984

    Re: Pulling a value from a matrix

    Hi. This will do what you want:

    =SMALL(INDEX($B$2:$G$5,MATCH(J2,$A$2:$A$5,0),),MIN(MAX(COLUMNS($B$2:$G$5)),COUNTIF(INDEX($B$2:$G$5,MATCH(J2,$A$2:$A$5,0),),"<"&$K$2)+1))
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    08-17-2019
    Location
    Orlando, FL
    MS-Off Ver
    Office 2010
    Posts
    7

    Re: Pulling a value from a matrix

    I think you left out that part again that returns the header row value from B1-G1, per earlier post. I tried adding this to it:

    =INDEX($B$1:$G$1,,

    But that did not work.

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,410

    Re: Pulling a value from a matrix

    I think you must have completely ignored my suggestion in post #8 which works as long as the data is correct. However, this line is not:

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    F
    G
    4
    Value 3
    1600
    1,500
    3,000
    6,500
    8,000
    11,000
    Sheet: Sheet1

    Cell B4 is clearly wrong - what should it be?

    The other problem is that you have provided sample workbooks that show the wrong answer, not the answer you are seeking, and that's confusing for your helpers.

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    1
    100
    200
    300
    400
    500
    600
    2
    Value 1
    6,250
    15,000
    30,000
    60,000
    90,000
    175,000
    Value 3
    1000
    100
    3
    Value 2
    5,500
    15,000
    34,000
    70,000
    120,000
    250,000
    4
    Value 3
    1000
    1,500
    3,000
    6,500
    8,000
    11,000
    5
    Value 4
    12,000
    35,000
    65,000
    100,000
    150,000
    215,000
    Sheet: Sheet1

    Excel 2016 (Windows) 32 bit
    L
    2
    =INDEX($B$1:$G$1,,MATCH(K2,OFFSET($B$1:$G$1,MATCH(J2,$A$2:$A$5,0),),1))
    Sheet: Sheet1
    Last edited by AliGW; 08-19-2019 at 01:21 AM.

  19. #19
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,984

    Re: Pulling a value from a matrix

    Oh dear. It really is time that I was taken out and shot. Try this!!

    =INDEX($B$1:$G$1,,MIN(MAX(COLUMNS($B$2:$G$5)),COUNTIF(INDEX($B$2:$G$5,MATCH(J2,$A$2:$A$5,0),),"<"&$K$2)+1))
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Matrix Multiplication in VBA and storing values in another matrix
    By anwid in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-06-2018, 06:23 AM
  2. [SOLVED] VBA to convert a coloured matrix into an equivalent matrix of 1's and 0's
    By The_Snook in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-27-2017, 01:12 PM
  3. [SOLVED] Pulling up value based on matching matrix
    By Shadmani in forum Excel General
    Replies: 5
    Last Post: 03-03-2016, 12:43 PM
  4. Replies: 2
    Last Post: 11-05-2014, 03:41 AM
  5. Pulling one slice from a 3D matrix
    By mattcschmidt in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-03-2014, 02:12 PM
  6. Replies: 2
    Last Post: 02-11-2014, 05:05 AM
  7. Pulling data from a matrix/table
    By tedium in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-23-2006, 07:37 AM

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