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 Feb 28th, 2002, 03:39 AM   #1
blayet
New Member
 
Join Date: Feb 2002
Posts: 4
Default


I am having problems altering the values of cells in a range that has been passed to a macro.

In "Writing excel macros" a code fragment used for this purpose is:

rng.Columns(1).Cells(1,1).Value = 1

but for me it simply gives a #Value error.

Any ideas what I am doing wrong?

Ben
blayet is offline   Reply With Quote
Old Feb 28th, 2002, 06:20 AM   #2
Derek
Board Regular
 
Join Date: Feb 2002
Location: Perth Australia
Posts: 1,567
Default

Hi Blayet

Cells(1,1) means the cell in row 1 column 1 (ie A1) so I'm not sure why you also need rng.Columns(1).
See if your macro works with only this bit of the code:
Cells(1, 1).Value = 1

Regards
Derek
Derek is offline   Reply With Quote
Old Feb 28th, 2002, 06:30 AM   #3
blayet
New Member
 
Join Date: Feb 2002
Posts: 4
Default

Yes I agree that rng.Cells(1,1).Value seems to make more sense. But it doesnt work either...


Ben
blayet is offline   Reply With Quote
Old Feb 28th, 2002, 06:42 AM   #4
Derek
Board Regular
 
Join Date: Feb 2002
Location: Perth Australia
Posts: 1,567
Default

Hi
I only used cells(1,1).Value = 1
I didn't use rng.
I guess that rng. is a declared variable somewhere at the beginning of your macro. This is not my strong point but I'm sure if you post a bit more of your code someone will know the answer.
Good Luck
Derek


Derek
Derek is offline   Reply With Quote
Old Feb 28th, 2002, 07:00 AM   #5
blayet
New Member
 
Join Date: Feb 2002
Posts: 4
Default

Derek

Yes. Rng is a variable of type Range, passed to macro as parameter. If it is not used then Cells(1,1).Value operates on the entire sheet.

Anyway, for clarity, a complete test macro is:

Public Function temp(rng As Range) As Variant
MsgBox rng.Cells(1, 1).Value
rng.Cells(1, 1).Value = 2
temp = "OK"
End Function

This will display value of first cell in the range and then attempt to change the value. The display part works fine - but I get a #value error when trying to set the cell. Is this operation not permitted? If not is there any other simple way of doing this.

Thanks for your help

Ben
blayet is offline   Reply With Quote
Old Feb 28th, 2002, 09:24 PM   #6
Derek
Board Regular
 
Join Date: Feb 2002
Location: Perth Australia
Posts: 1,567
Default

Ben
Sorry for delay (had to go to bed).
I am really out of my depth here, so you probably need to repost to get the attention of a vb wiz.
If its any use at all, without calling a function, I can use this sub to produce a message box giving the value of cell(1,1) in the declared range and then changing that value to 18.

Sub Derek()
Dim rng As Range
Set rng = Range("D4:F8")
MsgBox rng(1, 1).Value
rng(1, 1).Value = 18
End Sub

The following also seems to work using an input box:

Dim rng As Range
Set rng = Range("D4:F8")
rng(1, 1).Value = InputBox("What number do you want to replace " & rng(1, 1).Value & " in " & rng(1, 1).Address)
End Sub

Good luck
Derek

[ This Message was edited by: Derek on 2002-02-28 20:32 ]
Derek is offline   Reply With Quote
Old Feb 28th, 2002, 11:05 PM   #7
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-02-28 06:00, blayet wrote:
Yes. Rng is a variable of type Range, passed to macro as parameter. If it is not used then Cells(1,1).Value operates on the entire sheet.

Anyway, for clarity, a complete test macro is:

Public Function temp(rng As Range) As Variant
MsgBox rng.Cells(1, 1).Value
rng.Cells(1, 1).Value = 2
temp = "OK"
End Function

This will display value of first cell in the range and then attempt to change the value. The display part works fine - but I get a #value error when trying to set the cell. Is this operation not permitted? If not is there any other simple way of doing this.
Ben are you trying to exercute this function from a worksheet cell as a UDF ??
The function won't work if you are....
a worksheet Function cannot change another cells content.
To do what you want then change the procdure to a sub OR call the function from a sub.

eg
Sub test()
temp Range("A2:A3")
End Sub


Ivan
Ivan F Moala is offline   Reply With Quote
Old Mar 1st, 2002, 03:06 AM   #8
blayet
New Member
 
Join Date: Feb 2002
Posts: 4
Default

Ivan

You are correct I was using this function in a worksheet cell. A high level explanation of what I am really trying to do might be helpful: I want to write a subroutine that takes the values of a number of cells and calculates certain other values, which are then placed in another set of cells. The locations of the cells must be passed to the routine as arguments.

Initially, it seemed it would be convenient if I could specify both the input and output sets of cells as ranges, which are passed as arguments to a function in a worksheet cell. But it seems you cant work things this way. As you suggest I could use a sub, but as far as I know I would have to call this manually to perform a recalculation. I want a method that automatically recalculates when any input cell value changes. Also, it would be more convenient to select the relevant cell ranges graphically (as one can for range arguments to worksheet functions) rather than by editing the macro. Actually it is important that a single routine can be called multiple times operating on different cells each time - so it cant be hard coded for a specific set of input/output cells.

Maybe this gives a better idea of what I am trying to do. Any suggestions?

One approach that occurs to be is to write a general sub that does the computational work and lots of short subs that call the general sub with specific cell ranges to operate on. But this seems a bit awkward. I certainly dont want to have to manually run 10's of macros each time I want to recalculate the sheet. But I suppose I could write another sub that calls all the short subs, each of which calls the general sub...

I dont know what the best way of doing this is!

cheers

ben


[ This Message was edited by: blayet on 2002-03-01 02:08 ]
blayet 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 07:54 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