Re: Initializing variables
- From: "Don" <don87109@xxxxxxxxxxx>
- Date: Mon, 15 Dec 2008 12:21:56 -0500
Rick,
Thank you.
I tried it and it worked great.
Don
"Rick Rothstein" <rick.newsNO.SPAM@xxxxxxxxxxxxxxxxxx> wrote in message
news:%23QzDCXlXJHA.1328@xxxxxxxxxxxxxxxxxxxxxxx
Well, I didn't promise that my suggestion could always be used... I did
say "probably" and offered the conditions under which it would work. If
you still wanted to try the approach, and didn't mind using a Variant,
then you could do it this way...
Function Foo() As Variant
Static MyVariable As Variant
If IsEmpty(MyVariable) Then MyVariable = 10000
....
....
End Function
This works because Variants start off defaulted to Empty and will never be
Empty again unless specifically set to it. Of course, you could always use
another variable as an initialization switch too, you know.<g>
--
Rick (MVP - Excel)
"Don" <don87109@xxxxxxxxxxx> wrote in message
news:eboODOlXJHA.1328@xxxxxxxxxxxxxxxxxxxxxxx
Rick,
Thanks for the suggestion, but I don't think it works in my case. That's
because the normal range of values potentially stored in MyVariable is
zero.
However, maybe I could add a constant to any MyVariable values to avoid
zero. I just have to remember to subtract that constant anytime I want
the true value of MyVariable.
On second thought maybe it's easier just to have another variable as an
initialization switch.
Don
"Rick Rothstein" <rick.newsNO.SPAM@xxxxxxxxxxxxxxxxxx> wrote in message
news:ORsY35ZXJHA.5272@xxxxxxxxxxxxxxxxxxxxxxx
You probably don't need another variable to serve as an initialization
switch... you can probably just use the contents of the variable itself.
For example,
Function Foo() As Variant
Static MyVariable As Long
If MyVariable = 0 Then MyVariable = 10000
....
....
End Function
As long as MyVariable is never reset to 0, it will retain the last value
assigned to it whenever the function is called (the Static rather than
Dim statement sees to that).
--
Rick (MVP - Excel)
"Don" <don87109@xxxxxxxxxxx> wrote in message
news:%23RRIMmWXJHA.2444@xxxxxxxxxxxxxxxxxxxxxxx
Chip and Dana,
Thanks for the feedback. I'm amazed that you can't set a value on the
dim statement. Oh well, the variable does change so I'll have to add
code to somehow initialize it the first time the function is executed.
I guess I'll have to use another variable to serve as an initialization
switch. I'll set that variable to "one" after the main variable is
initialized and then use that "one" in an If statement to skip the
initialization code whenever the function is subsequently called.
Don
"Dana DeLouis" <ddelouis@xxxxxxxxxxxxx> wrote in message
news:uhfaVaWXJHA.760@xxxxxxxxxxxxxxxxxxxxxxx
can't find a way to do this on the Dim statement.
Just to mention...if the variable won't change, perhaps use a
constant.
The idea being that you dim it and set its value at the same time.
Const Pi As Double = 3.14159
= = =
Dana DeLouis
Don wrote:
I'm writing a Function and need to initialize a variable to say
10,000. I can't find a way to do this on the Dim statement. Of course
I can initialize it programmatically in the code section, but that
seems overly complicated. Is there a simple way to initialize
variables?
Bear in mind I only want to initialize once and not each time the
Function is invoked.
Don
.
- References:
- Initializing variables
- From: Don
- Re: Initializing variables
- From: Dana DeLouis
- Re: Initializing variables
- From: Don
- Re: Initializing variables
- From: Rick Rothstein
- Re: Initializing variables
- From: Don
- Re: Initializing variables
- From: Rick Rothstein
- Initializing variables
- Prev by Date: Re: Displaying a liste of Xi, Yi points
- Next by Date: Excel 2003 documents migration to 2007
- Previous by thread: Re: Initializing variables
- Next by thread: Re: Initializing variables
- Index(es):
Relevant Pages
|