Saturday, August 10, 2013

Compare value in a column and return corresponding value in the next column

Question:

( by Alzarath )


I'm far from an experienced spreadsheet user. Most of what I have done has been from searching and adapting.

I want my formula to (for example) scan the B column and, if one of the fields match a given field, it'll acquire the same row that it matches, but for C.

Here's an example, assuming A2 is the formula and A1 is the input:
B1 is 5, B2 is 17, B3 is 15, etc.
C1 is 0, C2 is 1000, C3 is 2500 etc.
If A1 is 500, A2 will be 5 (B1). If A1 is 1100, A2 will be 17 (B2), and so on.

What I currently have (keep in mind this doesn't follow the example) is this:

=if(P2<Reference!N4,Reference!M3,if(P2<Reference!N5,Reference!M4,if(P2<Reference!N6,Reference!M5,if(P2<Reference!N7,Reference!M6,if(P2<Reference!N8,Reference!M7,if(P2<Reference!N9,Reference!M8,if(P2<Reference!N10,Reference!M9,if(P2<Reference!N11,Reference!M10,if(P2<Reference!N12,Reference!M11,if(P2<Reference!N13,Reference!M12,if(P2<Reference!N14,Reference!M13,if(P2<Reference!N15,Reference!M14,if(P2<Reference!N16,Reference!M15,if(P2<Reference!N17,Reference!M16,if(P2<Reference!N18,Reference!M17,if(P2<Reference!N19,Reference!M18,if(P2<Reference!N20,Reference!M19,if(P2<Reference!N21,Reference!M20,if(P2<Reference!N22,Reference!M21,if(P2<Reference!N23,Reference!M22,if(P2<Reference!N24,Reference!M23,if(P2<Reference!N25,Reference!M24,if(P2<Reference!N26,Reference!M25,if(P2<Reference!N27,Reference!M26,if(P2<Reference!N28,Reference!M27,if(P2<Reference!N29,Reference!M28,if(P2<Reference!N30,Reference!M29,if(P2<Reference!N31,Reference!M30,if(P2<Reference!N32,Reference!M31,Reference!M32)))))))))))))))))))))))))))))

Basically a big if-then-else-then-else-etc statement. I'd very much like a way to crop this down significantly, if it's possible, and make it so I only have to adjust a value or two in case the table were expanded, instead of adding all of the values manually.


Solution:

Have a look at the following screenshot of Sheet "Reference":


And following is the screenshot of Sheet2:



I have inserted your formula in Cell M2.

And I have inserted the following formula in Cell M3:

=query('Reference'!M3:N;"select M where N<"&P2&"order by N desc limit 1")


I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.



I also take up private and confidential projects:
If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 

Thanks,

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I am highly appreciative. This worked almost flawlessly. Only thing I changed is made it '<=' so it adjusted /on/ the value. I apologize if I didn't make that clear enough. Very impressive work, thank you.

    ReplyDelete
  3. After some time of examining it, I think I'm starting to understand what the formula does. I'm having an issue, though. I duplicated it and am trying to get it to work on another table. Here's what I have:

    =query(Reference!A3:J;"select E where A="&C3&" order by A desc limit 1")

    The A column and C3 are strings. The formula seems to be trying to interpret C3 into a column, looking for said column instead of searching for it in the table.

    ReplyDelete
    Replies
    1. After much experimentation, I discovered I had to add single quotes outside of the double quotes.

      Delete
    2. Yes, you need to add single quotes for text values... and for numeric values you don't need to add single quotes..

      Delete