MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Aug 26th, 2004, 11:33 AM   #1
stakar
 
Join Date: Mar 2004
Posts: 251
Default Lookup for a value in Unsorted data

Hi!
I have 2 columns A & B that their data are like these
A B
500 0
501 1
502 5
503 0
504 2
505 2
506 4
507 0
508 3
509 5
510 1
511 0

I want to keep the data on column B that way (unsorted) but i want to find the first position the 0, the 1, the 5 etc. is, starting from bottom to top. and for return to get the value of the column A.
So if i want to find the 0 i ll get as result the 511
So if i want to find the 5 i ll get as result the 509
So if i want to find the 4 i ll get as result the 506 and goes on.

Thanks in advance
Stathis
stakar is offline   Reply With Quote
Old Aug 26th, 2004, 11:48 AM   #2
Aladin Akyurek
MrExcel MVP
 
Aladin Akyurek's Avatar
 
Join Date: Feb 2002
Location: The Hague
Posts: 40,228
Default Re: Lookup for a value in Unsorted data

Quote:
Originally Posted by stakar
Hi!
I have 2 columns A & B that their data are like these
A B
500 0
501 1
502 5
503 0
504 2
505 2
506 4
507 0
508 3
509 5
510 1
511 0

I want to keep the data on column B that way (unsorted) but i want to find the first position the 0, the 1, the 5 etc. is, starting from bottom to top. and for return to get the value of the column A.
So if i want to find the 0 i ll get as result the 511
So if i want to find the 5 i ll get as result the 509
So if i want to find the 4 i ll get as result the 506 and goes on...
******** ******************** ************************************************************************>
Microsoft Excel - Book1___Running: 11.0 : OS = Windows Windows 2000
(F)ile (E)dit (V)iew (I)nsert (O)ptions (T)ools (D)ata (W)indow (H)elp (A)bout
=

A
B
C
D
E
1
XY*YX
2
5000*0511
3
5011*1510
4
5025*2505
5
5030*3508
6
5042*4506
7
5052*5509
8
5064***
9
5070***
10
5083***
11
5095***
12
5101***
13
5110***
Sheet1*

[HtmlMaker 2.42] To see the formula in the cells just click on the cells hyperlink or click the Name box
PLEASE DO NOT QUOTE THIS TABLE IMAGE ON SAME PAGE! OTHEWISE, ERROR OF JavaScript OCCUR.


The formula in E2, which is copied down, is:

=LOOKUP(2,1/($B$2:$B$13=D2),$A$2:$A$13)
Aladin Akyurek is offline   Reply With Quote
Old Aug 26th, 2004, 01:01 PM   #3
stakar
 
Join Date: Mar 2004
Posts: 251
Default

Thanks a lot!!
It was exactly what i wanted to do!!

Stathis
stakar is offline   Reply With Quote
Old Aug 26th, 2004, 01:48 PM   #4
just_jon
MrExcel MVP
 
just_jon's Avatar
 
Join Date: Sep 2002
Location: Alabama/State of Disarray
Posts: 10,473
Default Re: Lookup for a value in Unsorted data

Now, that's slick.
__________________
just_jon
Book of the Month: I'm Not Really an MVP, I Just Play One on TV [j. jon, 2004]
just_jon is offline   Reply With Quote
Old Aug 26th, 2004, 02:14 PM   #5
RalphA
 
Join Date: May 2003
Location: Katy, Texas
Posts: 3,829
Default Re: Lookup for a value in Unsorted data

Just Jon has expressed his appreciation for Aladin's solution! Me, I can see the brilliant results, but, when I find the description for LOOKUP, I find:
=LOOKUP(lookup_value,lookup_vector,result_vector).

Would you be so kind as to explain the logic of your formula (for E2)?:
=LOOKUP(2,1/($B$2:$B$13=D2),$A$2:$A$13)

From the description, your formula would seem to indicate that:
lookup_value=2
lookup_vector = 1/($B$2:$B$13=D2)

I just can't understand what is going on here???

Thanks.
RalphA is offline   Reply With Quote
Old Aug 26th, 2004, 03:49 PM   #6
fairwinds
MrExcel MVP
 
fairwinds's Avatar
 
Join Date: May 2003
Posts: 8,462
Default Re: Lookup for a value in Unsorted data

Quote:
Originally Posted by RalphA
Just Jon has expressed his appreciation for Aladin's solution! Me, I can see the brilliant results...


Well it's briliant!

1/($B$2:$B$13=D2) is the lookup array, an array that will look something like:

#DIV/0!, #DIV/0!, 1, #DIV/0!, 1, #DIV/0!

Looking up 2 in that will return the position of the last "1".
__________________
"Fair Winds and Following Seas"
fairwinds is offline   Reply With Quote
Old Aug 27th, 2004, 02:26 AM   #7
RalphA
 
Join Date: May 2003
Location: Katy, Texas
Posts: 3,829
Default Re: Lookup for a value in Unsorted data

Thank you, Fairwinds! I understood it, pretty much, to mean that it returns the error code for division by 0 when the value in parenthesis is false (a 0), and it returns a 1 for 1/1 when true. I usually have only a vague glimmer of understanding how the handling of arrays is done, but, this time, I do believe I was able to follow your detailed explanations. Thanks again!

However... (sorry), I still don't understand how it can report the position of the fourth (the last) 0 in the data range. Also, how does it determine the correct answer for the one instance of 4?
RalphA is offline   Reply With Quote
Old Aug 27th, 2004, 09:47 AM   #8
fairwinds
MrExcel MVP
 
fairwinds's Avatar
 
Join Date: May 2003
Posts: 8,462
Default Re: Lookup for a value in Unsorted data

Looking up 2 here is the same principal as using "BigNum". I'm sure you can find an explanation if you search for Aladins posts on BigNum.
__________________
"Fair Winds and Following Seas"
fairwinds is offline   Reply With Quote
Old Aug 27th, 2004, 11:36 AM   #9
Aladin Akyurek
MrExcel MVP
 
Aladin Akyurek's Avatar
 
Join Date: Feb 2002
Location: The Hague
Posts: 40,228
Default Re: Lookup for a value in Unsorted data

Quote:
Originally Posted by RalphA
...However... (sorry), I still don't understand how it can report the position of the fourth (the last) 0 in the data range. Also, how does it determine the correct answer for the one instance of 4?
Quote:
Originally Posted by RalphA
...However... (sorry), I still don't understand how it can report the position of the fourth (the last) 0 in the data range. Also, how does it determine the correct answer for the one instance of 4?
Fairwinds's explanation/exposition is just about right.

=LOOKUP(2,1/($B$2:$B$13=D2),$A$2:$A$13)

is structured as:

LOOKUP(LookupValue,LookupVector,ResultVector)

where a vector can be an array like {2,3,7,9} or range object like X3:X6.

The way the formula is set up exploits the fact that

(A)

LOOKUP(n,Ref)
VLOOKUP(n,Ref,1,1)
INDEX(Ref,MATCH(n,Ref,1)

will all return the last numerical value from Ref when n is a number that cannot occur in Ref, an example use of which is:

=LOOKUP(9.99999999999999E+307,A:A);

And the fact that

(B)

the lookup functions ignore error values.

Since the LookupVector in the formula of interest, that is,

1/($B$2:$B$13=D2),

is an array object, I should also add the fact that

(C)

the LOOKUP function is capable of returning computed arrays, without control+shift+enter (SumProduct is another example).

Understanding (A) is essential. It's the topic of a discussion in:

http://tinyurl.com/5l4j7

Since

=LOOKUP(9.99999999999999E+307,A2:A25)

returns the last numerical value from A2:A25, which is not perse the last value, the following is an obvious extension:

=LOOKUP(9.99999999999999E+307,A2:A25,B2:B25)

will return the value from B2:B25 which is associated with the last numerical value in A2:A25. (9.99999999999999E+307 is often referred to as BigNum.)

Given the foregoing,

=LOOKUP(2,1/($B$2:$B$13=D2),$A$2:$A$13)

becomes intelligible for:

The lookup value 2 is a BigNum with respect to 1/($B$2:$B$13=D2), because 1 divided by any number cannot be equal to or greater than 2. One can replace 2, if so desired, with the BigNum itself (that is: 9.99999999999999E+307).

($B$2:$B$13=D2)

is a conditional that is bound to evaluate to an array consisting of logical values like:

{TRUE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;TRUE}

Then:

1/{TRUE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;TRUE}

Since 1 and 0 are Excel's numerical equivalents of TRUE and FALSE under coercion, we get a calculated array like:

{1;#DIV/0!;#DIV/0!;1;#DIV/0!;#DIV/0!;#DIV/0!;1;#DIV/0!;#DIV/0!;#DIV/0!;1}

Since the last numerical value is the 12th item in the foregoing array, LOOKUP will retrieve the 12th item from $A$2:$A$13, the ResultVector.

Let's take up the formula which corresponds to D6 housing 4 (See the exhibit in my original post)...

=LOOKUP(2,1/($B$2:$B$13=D6),$A$2:$A$13)

===>

=LOOKUP(2,1/({0;1;5;0;2;2;4;0;3;5;1;0}=4),{500;501;502;503;504;505;506;507;508;509;510;511})

===> (after the divison)

=LOOKUP(2,{#DIV/0!;#DIV/0!;#DIV/0!;#DIV/0!;#DIV/0!;#DIV/0!;1;#DIV/0!;#DIV/0!;#DIV/0!;#DIV/0!;#DIV/0!},{500;501;502;503;504;505;506;507;508;509;510;511})

As can be seen, the last numerical value (the last instance of 1) is the 7th item. As it so happens, the last numerical value here is also the only numerical value. Hereafter LOOKUP proceeds to fetch the 7th item from

{500;501;502;503;504;505;506;507;508;509;510;511}

which is: 506.

Hope the foregoing is filling in fairwinds's correct exposition at those places where you wanted to have more info.
Aladin Akyurek is offline   Reply With Quote
Old Aug 27th, 2004, 12:50 PM   #10
fairwinds
MrExcel MVP
 
fairwinds's Avatar
 
Join Date: May 2003
Posts: 8,462
Default Re: Lookup for a value in Unsorted data

Aladin!
Thanks for this excelent explanation.

Now, next time I only need to post a link.
__________________
"Fair Winds and Following Seas"
fairwinds is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 05:00 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes