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 18th, 2002, 12:34 PM   #1
Sam40mUK
Board Regular
 
Join Date: Mar 2002
Location: North Wales Coast, UK
Posts: 95
Default

Evening All,
I am looking to find a Value in a list and then move along the row, say 5 cells to the right and paste a value into that cell.
I have looked at a few ways of going about this but is there a 'smarter' way than considering Looping through the list of Values?
Any comments, well most(!), appreciated.
Cheers
S
Sam40mUK is offline   Reply With Quote
Old Apr 18th, 2002, 12:38 PM   #2
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

If you're using VBA to do this, have a look at the "Find" function in VBA help. There's a reasonable example there. It's definitely faster than looping through cells. (combine this with "Offset" and you'll be good to go)
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 18th, 2002, 01:52 PM   #3
Sam40mUK
Board Regular
 
Join Date: Mar 2002
Location: North Wales Coast, UK
Posts: 95
Default

Hi Mark,
Thank you for your swift response.
I have had a look at the 'Find Method' Example in VBA Help and have, once again, discovered why I deal with Accounts rather than VBA programming!
Do you happen to know of any other source of relatively easy examples and explanation so I may try to get my head around the Find Method?
Thanks once again
S

[ This Message was edited by: Sam40mUK on 2002-04-18 15:42 ]
Sam40mUK is offline   Reply With Quote
Old Apr 19th, 2002, 10:22 AM   #4
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Yeah, I said that the help file example was reasonable. I don't like it myself, but it's usually a decent place to start. I've knocked up an example for you and I hope it makes enough sense to get you started. I started a new workbook and on Sheet1 I put in the values 1 to 15 in column A starting in A1. I then made this code to find the value "10" and put a message in a box 5 columns to the right.


'Declare variables
Dim oTargetRange As Range 'This will be the range we're searching.
Dim sTargetText As String 'This will be text that we're trying to find
Dim oFoundRange As Range 'This will be the range (cell) that contains the "sTargetText"
Dim iColumnOffset As Integer 'This will be the number of columns we want to offset by
Dim sColumnOffsetText As String 'This is the text you want to put in the offset column if the target text is found

'Initialise variables
Set oTargetRange = Sheets("Sheet1").UsedRange.Columns("A")
sTargetText = "10"
iColumnOffset = 5
sColumnOffsetText = "Very Long Variable Name"

'Begin Error trapping (for when we screw up)
On Error GoTo ERR_NOT_FOUND

Set oFoundRange = oTargetRange.Find(What:=sTargetText, LookIn:=xlValues, lookat:=xlWhole)

MsgBox "The text " & Chr(34) & sTargetText & Chr(34) & "was found in " & oFoundRange.Address & "."
oFoundRange.Offset(0, iColumnOffset).Value = sColumnOffsetText


Exit Sub
ERR_NOT_FOUND:
MsgBox "The text " & Chr(34) & sTargetText & Chr(34) & " could not be found in the specified range.", vbInformation, "Not Found"

End Sub


It shouldn't take too much for you to adapt it to your specific needs. (I hope).

HTH
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 19th, 2002, 02:17 PM   #5
Sam40mUK
Board Regular
 
Join Date: Mar 2002
Location: North Wales Coast, UK
Posts: 95
Default

Firstly thanks Mark,
I appreciate your time in sorting out a further example for me.
I did spend some time looking at VBA Help and found it for a novice rather .. there you go .. take it or leave it!
I am sure that others on this board will agree that a well explained example with the ability to ask, what maybe considered 'silly' questions is an immensely useful way of trying to understand Excel and glimpsing the use of VBA in the 'real world'.
Thanks one again
S
Sam40mUK is offline   Reply With Quote
Old Apr 19th, 2002, 02:27 PM   #6
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

No problems at all. I know that some parts of the help file are better than others and if it was perfect then this board wouldn't exist.

Also, I wasn't sure of your expertise and usually I don't do lazy answers, but I think I had one eye on the golf course yesterday. (additionally, we have all went through what you're going through)
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 19th, 2002, 03:22 PM   #7
heather
New Member
 
Join Date: Apr 2002
Posts: 8
Default

I really liked your example, the sTargettext you set is for a set expression, if I wanted the sTargetText to be based off a cell in another worksheet, how would that look?

sTargetText = worksheets("SheetA").[J4].Value? Range? or something like that?
heather is offline   Reply With Quote
Old Apr 22nd, 2002, 02:37 PM   #8
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Quote:
On 2002-04-19 14:22, heather wrote:
I really liked your example, the sTargettext you set is for a set expression, if I wanted the sTargetText to be based off a cell in another worksheet, how would that look?

sTargetText = worksheets("SheetA").[J4].Value? Range? or something like that?
Heather,

I just saw your post today, you were right first time, but you may have figred that out by now:


sTargetText = worksheets("SheetA").[J4].Value


Although personally, I don't really like using e.g. [J4] for ranges. I'm too used to using "Range" like this:


sTargetText = worksheets("SheetA").Range("J4").Value


...but I have used the []'s in posts here because it's just too damned tempting to use it in a small snippet of code.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien 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:06 AM.


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