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 Apr 8th, 2002, 10:03 AM   #1
pleasehelpme
New Member
 
Join Date: Apr 2002
Posts: 5
Default

If I have 2 columns as follows:

20 cat
39 dog
40 cow
39 fox
50 rat
39 mouse

Is there a way I can lookup "39" and return the first 39=dog and the next 39=fox and the next 39=mouse etc..?
pleasehelpme is offline   Reply With Quote
Old Apr 8th, 2002, 10:13 AM   #2
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Change the reference of VLOOKUP's table_array to exclude the previously found item.
Mark W. is offline   Reply With Quote
Old Apr 8th, 2002, 10:21 AM   #3
pleasehelpme
New Member
 
Join Date: Apr 2002
Posts: 5
Default

I was hoping I could keep the table array constant. Is there another way around this problem?
pleasehelpme is offline   Reply With Quote
Old Apr 8th, 2002, 10:26 AM   #4
hollifd
Board Regular
 
Join Date: Apr 2002
Location: Louisville, Ohio
Posts: 247
Default

I am not sure if this helps you, but you may be able to use "AutoFilter" under the menu...

Data
Filter
Autofilter

This would allow you to show only the items that have "39" in the first column.

The only other idea that I have is to write some VBA code to ask the user for a value like "39" and then have the VBA code find all cells in column A that have "39" and write the results in some other cells. This should not be too dificult.

I hope this helps at least a little.

David
hollifd is offline   Reply With Quote
Old Apr 8th, 2002, 10:32 AM   #5
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Quote:
On 2002-04-08 09:21, pleasehelpme wrote:
I was hoping I could keep the table array constant. Is there another way around this problem?
You could setup your table array like...

{20,"cat",1
;39,"dog",2
;40,"cow",3
;39,"fox",4
;50,"rat",5
;39,"mouse",6}

...and enter the array formula...

{=VLOOKUP(39,OFFSET(table_array,F1,),{2,3},0)}

...into cells E2:F2 (leaving F1 blank) and copying down to cells E4:F4.

Note 1: Substitute an absolute cell reference "table_array"

Note 2: Array formulas must be entered using the Control+Shift+Enter key combination. The outermost braces, { }, are not entered by you -- they're supplied by Excel in recognition of a properly entered array formula.

[ This Message was edited by: Mark W. on 2002-04-08 09:33 ]
Mark W. is offline   Reply With Quote
Old Apr 8th, 2002, 11:06 AM   #6
Aladin Akyurek
MrExcel MVP
 
Aladin Akyurek's Avatar
 
Join Date: Feb 2002
Location: The Hague
Posts: 50,317
Default

Quote:
On 2002-04-08 09:03, pleasehelpme wrote:
If I have 2 columns as follows:

20 cat
39 dog
40 cow
39 fox
50 rat
39 mouse

Is there a way I can lookup "39" and return the first 39=dog and the next 39=fox and the next 39=mouse etc..?
Lets say that A1:B7 houses the sample data you provided, with labels added:

{"Field1","Field2";
20,"cat";
39,"dog";
40,"cow";
39,"fox";
50,"rat";
39,"mouse"}

In D1 enter:

=MATCH(9.99999999999999E+307,Sheet1!A:A)-ROW(1:1)

This formula computes the number of actual data records in the data range dynamically.

In D2 enter: 39 [ which is your lookup value ]

In E2 enter:

=IF($D$2,VLOOKUP($D$2,OFFSET($A$2,0,0,$D$2,2),2,0),"")

In E3 enter and copy down until no more retrieval occurs, associated with the value in D2:

=IF(AND($D$2,COUNTIF(OFFSET($A$2,0,0,$D$1,1),$D$2)>COUNTA($E$2:E2)),INDEX(OFFSET($B$2,MATCH(E2,OFFSET($B$2,0,0,$D$1,1),0),0,$D$1,1),MATCH($D$2,OFFSET($A$2,MATCH(E2,OFFSET( $B$2,0,0,$D$1,1),0),0,$D$1,1),0)),"")

This is what you're going to see in the results area:

{6,"";
39,"dog";
"","fox";
"","mouse"}


Aladin
Aladin Akyurek is offline   Reply With Quote
Old Apr 9th, 2002, 05:54 AM   #7
pleasehelpme
New Member
 
Join Date: Apr 2002
Posts: 5
Default

Thanks for that Aladin,
I am most impressed, however there are 2 points that may be of use:

Point 1/ correct me if I am wrong, but should part of your last message have read:

=IF($D$2,VLOOKUP($D$2,OFFSET($A$2,0,0,$D$1,2),2,0),"")

rather than:

=IF($D$2,VLOOKUP($D$2,OFFSET($A$2,0,0,$D$2,2),2,0),"")


Point 2/Is it possible to possible to overcome the problem of having duplicate data in "Field 2". For example, if we add on to the bottom of the existing data:

39, "rat"
39, "fox"

The results do NOT give:
{6,"";
39,"dog";
"","fox";
"","mouse";
"","rat";
"","fox"}

but instead give:
{6,"";
39,"dog";
"","fox";
"","mouse";
"","rat";
"","mouse"}

Many thanks
Nick
pleasehelpme is offline   Reply With Quote
Old Apr 9th, 2002, 05:58 AM   #8
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi


If you have a look here: http://www.ozgrid.com/VBA/TwoColLkUp.htm you will find this UDF with full instructions.


Code:
Function FindNth(Table As Range, Val1 As Variant,Val1Occrnce As Integer, _
                  Val2 As Variant,Val2Col As Integer, ResultCol As Integer)

'''''''''''''''''''''''''''''''''''''''
'Written by OzGrid Business Applications
'www.ozgrid.com

'Finds the N'th value in the first Column of a table that has a stated _
 value on the same row in another Column.
'''''''''''''''''''''''''''''''''''''''

Dim i As Integer
Dim iCount As Integer
Dim rCol As Range

	For i = 1 To Table.Rows.Count
		If Table.Cells(i, 1) = Val1 And _
			Table.Cells(i, Val2Col) = Val2 Then
			iCount = iCount + 1
		End If

		If iCount = Val1Occrnce Then
			FindNth = Table.Cells(i, ResultCol)
			Exit For
		End If
	Next i
End Function

Dave Hawley is offline   Reply With Quote
Old Apr 9th, 2002, 07:03 AM   #9
Derek
Board Regular
 
Join Date: Feb 2002
Location: Perth Australia
Posts: 1,567
Default

Hi There

There is a very simple solution to this type of problem.

There are many elaborations you can make but the basic idea is this:
You insert a column with a formula which sequences the item you want to list.
You then do a vlookup on this column. Try this on your example

Put your numbers in Column B starting with B2 (keep A1 & A2 blank)
Put your names (cat, dog etc) in Column C starting with C2
In A2 paste this formula and scroll down =IF(B2=$E$1,1+COUNT($A$1:A1),"")
In E2 paste this formula and scroll down =VLOOKUP(ROW(E1),A:C,3,FALSE)
Now type 39 in cell E1 and the names associated with 39 will list below

If you want you can modify the formula so that #N/A does not show. Just change the formula in E2 to
=IF(COUNT(A:A)>=ROW(E1),VLOOKUP(ROW(E1),A:C,3,FALSE),"")

It is also possible, with modification, to obtain the answers from data in another sheet

Regards

Derek


[ This Message was edited by: Derek on 2002-04-09 07:39 ]
Derek is offline   Reply With Quote
Old Apr 9th, 2002, 07:49 AM   #10
Aladin Akyurek
MrExcel MVP
 
Aladin Akyurek's Avatar
 
Join Date: Feb 2002
Location: The Hague
Posts: 50,317
Default


Thanks for that Aladin,
I am most impressed,


Thanks. It's mutual. Happy to see that you understood the system.

however there are 2 points that may be of use:

Point 1/ correct me if I am wrong, but should part of your last message have read:

=IF($D$2,VLOOKUP($D$2,OFFSET($A$2,0,0,$D$1,2),2,0),"")

rather than:

=IF($D$2,VLOOKUP($D$2,OFFSET($A$2,0,0,$D$2,2),2,0),"")


Yep. Just visually confounded D1 & D2. I should have put that MATCH formula in C2 to avoid that visual trap..


Point 2/Is it possible to possible to overcome the problem of having duplicate data in "Field 2". For example, if we add on to the bottom of the existing data:

39, "rat"
39, "fox"

The results do NOT give:
{6,"";
39,"dog";
"","fox";
"","mouse";
"","rat";
"","fox"}

but instead give:
{6,"";
39,"dog";
"","fox";
"","mouse";
"","rat";
"","mouse"}


That's right. The formulas assume the uniqueness of values in column B. The addition boils down to having duplicate records. You could eliminate such duplicate records using Advanced Filter.

Aladin
Aladin Akyurek 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 -4. The time now is 04:12 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 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