Question about setting a variable

Mr930

Well-known Member
Joined
Aug 31, 2006
Messages
585
I had some code that worked fine where I set a variable using its range name like this:

[Constraint1] = szTemp

The function that contained this code was called and triggered by a worksheet change event.

I now have it set up that the function is called by a cell formula, and now when that line of code is executed, everything stops cold. When I step through the debugger the active line just goes away as if I had reached the end of my code.

The only change I can think of is the way the function is called and I am not sure if that is the culprit or not. Is there some rule for setting variables by using their range name?

thanks
Fred Emmerich
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Fred

You can't use a worksheet function to set the value of a cell, you can only return a value to the cell it's in.
 
Upvote 0
I now have it set up that the function is called by a cell formula

A function (any function, built in or VBA UDF) when called from within a cell formula CANNOT change the value or format of any other cell.
It can only return a value to the cell that the formula is entered in.

Sorry.
 
Upvote 0
The return value was just a placeholder. In the function itself I change multiple ranges. I have a couple of lines that erase ranges by name and even though the lines execute fine, the range is not cleared (just discovered this). Originally the ranges I was messing with were on the same sheet as the code, now they are not and I am wondering if that is the problem. I thought named ranges were workbook level variables, are they worksheet level?

thanks



quOTE=Norie;2714585]Fred

You can't use a worksheet function to set the value of a cell, you can only return a value to the cell it's in.[/QUOTE]
 
Upvote 0
this line

[Constraint1] = szTemp

Is NOT setting a variable.

It is placing the value of the variable szTemp into the Range Named Constraint1.

Therefor, it cannot be done when the function is called from a cell formula.
Because cell formulas CANNOT change the value of OTHER cells,
They can only return a value to the cell the formula is entered in.
 
Upvote 0
Okay, it finally sunk in. I leanred something new today.

thanks

Hey, you know what...
I just learned something new too...

I didn't know you could refer to a named range like that
[Constraint1]

I thought you could only do it like
Range("Constraint1")



You never stop learning...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,214
Members
452,895
Latest member
BILLING GUY

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top