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 4th, 2002, 09:57 AM   #1
Adrae
Board Regular
 
Join Date: Feb 2002
Location: Chicago, IL USA
Posts: 306
Default

I need a macro or formula that will do the following:

1. If the value in Sheet1 cell A1 is found in cells in column A of Sheet2 where the value in column D is product in one cell and service in another then Sheet1 cell f1 = "Both".

2. If the value in Sheet1 cell A1 is found in column A of sheet two and the corresponing values in column D are only product then f1 = "Product.

The trick is that the value that is being search will appear more than once on Sheet 2. If different values are found in column D for the same value in column A then, the condition in #1 should apply.


I hope this all makes sense.
Can anyone get me started?
Adrae is offline   Reply With Quote
Old Apr 4th, 2002, 11:13 AM   #2
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

Try the following:

Dim FirstValue, SecondValue As String
On Error GoTo 1
Set c = Worksheets("Sheet2").Columns("A").Find(Worksheets("Sheet1").Range("a1").Value, LookIn:=xlWhole)
FirstValue = Worksheets("Sheet2").Range(c.Address).Offset(0, 3).Value
Set c = Worksheets("Sheet2").Columns("A").FindNext(c)
SecondValue = Worksheets("Sheet2").Range(c.Address).Offset(0, 3).Value
If FirstValue = SecondValue Then
Worksheets("Sheet1").Range("F1").Value = "Both"
Else: Worksheets("Sheet1").Range("F1").Value = "Product"
End If
Exit Sub
1 MsgBox "Value not found"
__________________
Kind regards,

Al Chara
Al Chara is offline   Reply With Quote
Old Apr 4th, 2002, 11:14 AM   #3
Anne Troy
MrExcel MVP
 
Anne Troy's Avatar
 
Join Date: Feb 2002
Location: Allentown, PA
Posts: 2,512
Default

You don't need code to produce these results. Check out the MyVlookup file at:

http://www.thewordexpert.com/tipwarez.htm#MyVlookup
__________________
~Anne Troy
Anne Troy is offline   Reply With Quote
Old Apr 4th, 2002, 11:22 AM   #4
Russell Hauf
MrExcel MVP
 
Russell Hauf's Avatar
 
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
Default

Actually, I think code is necessary in this case due to the multiple matching values in the second sheet.

Also, can there be more than 2 values? It sounded like there could be more than 2 "matches" for a given value.
Russell Hauf is offline   Reply With Quote
Old Apr 4th, 2002, 11:48 AM   #5
Adrae
Board Regular
 
Join Date: Feb 2002
Location: Chicago, IL USA
Posts: 306
Default

Thanks - This code will certainly get me going.

How can I make it into a For-Next statement. So instead of saying, A1 of Sheet 1, I would say For Each Cell in Selection.

Also, what if the value in column A on Sheet 2 is repeated more than two times. For example, if the first occurence is equal to the second but not equal to the third or fourth, etc. Then the value will be ...

Is VBA capable of a lookup this complex?

Thanks Again
Adrae is offline   Reply With Quote
Old Apr 4th, 2002, 12:14 PM   #6
Adrae
Board Regular
 
Join Date: Feb 2002
Location: Chicago, IL USA
Posts: 306
Default

OK - I tried to modify this with a For-Next statement, but it isn't working. I get the error msg box at the end. Can anyone tell me what I am doing wrong:

Sub ProductServiceBoth()
Dim FirstValue, SecondValue As String
Dim Cel As Range
On Error GoTo 1
For Each Cel In Selection
Set c = Worksheets("Sheet2").Columns("A:A").Find(Worksheets("Sheet1").Range(Cel).Value, LookIn:=xlWhole)
FirstValue = Worksheets("Sheet2").Range(c.Address).Offset(0, 3).Value
Set b = Worksheets("Sheet2").Columns("A:A").FindNext(c)
SecondValue = Worksheets("Sheet2").Range(b.Address).Offset(0, 3).Value
If FirstValue = SecondValue Then
Worksheets("Sheet1").Range(Cel.Address).Offset(0, 3).Value = "Product"
Else: Worksheets("Sheet1").Range(Cel.Address).Offset(0, 3).Value = "Both"
End If
Next Cel
Exit Sub
1 MsgBox "Value not found"
End Sub


[ This Message was edited by: Adrae on 2002-04-04 11:15 ]
Adrae is offline   Reply With Quote
Old Apr 4th, 2002, 12:51 PM   #7
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

Quote:
Set c = Worksheets("Sheet2").Columns("A:A").Find(Worksheets("Sheet1").Range(Cel).Value, LookIn:=xlWhole)
This should read as follows:
Set c = Worksheets("Sheet2").Columns("A:A").Find(Worksheets("Sheet1").Range(Cel.address).Value, LookIn:=xlWhole)
__________________
Kind regards,

Al Chara
Al Chara is offline   Reply With Quote
Old Apr 4th, 2002, 01:00 PM   #8
Adrae
Board Regular
 
Join Date: Feb 2002
Location: Chicago, IL USA
Posts: 306
Default

Thanks Al. It works now.

I have one last issue to solve. As this code is, it only looks at the first two occurrences. I want it to continue looking if the values are the same to make sure no others that are different occur. If the first two or any values after that are different from the first, then the value can be assigned immediately, no need to look further. If they are are equal, I want it to continue looking until the end of the used cell in the column. How can I tell it to keep looking if the values are the same?

Thanks again
Adrae is offline   Reply With Quote
Old Apr 4th, 2002, 02:09 PM   #9
Adrae
Board Regular
 
Join Date: Feb 2002
Location: Chicago, IL USA
Posts: 306
Default

Perhaps a Do While Loop? I've been trying to code this but must be missing something. Either it seems not to run at all or runs interminably. Would a loop of this sort work to solve my final issue and if so, how would I insert the code into my existing code?
Adrae is offline   Reply With Quote
Old Apr 4th, 2002, 03:09 PM   #10
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

Try the following:

Dim FirstValue, SecondValue, LastAddress As String
Dim Cel As Range
Dim counter As Integer
Dim c As Object
On Error GoTo 2
LastAddress = "$A$" & WorksheetFunction.CountA(Worksheets("Sheet2").Columns(1)) + 1
For Each Cel In Selection
Set c = Worksheets("Sheet2").Columns("A:A").Find(Worksheets("Sheet1").Range(Cel.Address).Value, LookIn:=xlWhole, SearchOrder:=xlByColumns)
FirstValue = Worksheets("Sheet2").Range(c.Address).Offset(0, 3).Value
Set c = Worksheets("Sheet2").Columns("A:A").FindNext(c)
SecondValue = Worksheets("Sheet2").Range(c.Address).Offset(0, 3).Value
If FirstValue <> SecondValue Then
Worksheets("Sheet1").Range(Cel.Address).Offset(0, 3).Value = "Both"
GoTo 1
Else:
Do
Set c = Worksheets("Sheet2").Columns("A:A").FindNext(c)
SecondValue = Worksheets("Sheet2").Range(c.Address).Offset(0, 3).Value
If FirstValue <> SecondValue Then
Worksheets("Sheet1").Range(Cel.Address).Offset(0, 3).Value = "Both"
GoTo 1
End If
Loop Until c.Address = LastAddress Or c.Address = "$A$2"
Worksheets("Sheet1").Range(Cel.Address).Offset(0, 3).Value = "Product"
1 End If
counter = counter + 1
Next Cel
Exit Sub
2 MsgBox "Value not found"

Its not that clean, but I didn't feel like rewriting everything. It should work as long as your data starts in cell A2.
__________________
Kind regards,

Al Chara
Al Chara 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 06:57 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