Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Creating a static function

I'm not sure if i'm doing something that doesn't work, but why can't i seem
to get a static function to work the same as a static sub.

If i run the following code a few times, and i always use the number 1 as x,
i would think that the function should retain the previous value of "test",
and then just increment it by 1, but this isn't happening. "Test" always
returns as 1. I want this to work as a counter that other subs will pull from.

Static Function test(x As Integer) As Integer
test = test + x
End Function


thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Creating a static function

There's a good example in the VBA Help.
Static applies to variables, not to Functions.

--
Tim Williams
Palo Alto, CA


"Brite" wrote in message ...
I'm not sure if i'm doing something that doesn't work, but why can't i seem
to get a static function to work the same as a static sub.

If i run the following code a few times, and i always use the number 1 as x,
i would think that the function should retain the previous value of "test",
and then just increment it by 1, but this isn't happening. "Test" always
returns as 1. I want this to work as a counter that other subs will pull from.

Static Function test(x As Integer) As Integer
test = test + x
End Function


thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
Jay Jay is offline
external usenet poster
 
Posts: 671
Default Creating a static function

Hi Brite -

A preserved variable can't have the same name as the function. In other
words, all variables are preserved in a Static function (as you noted) except
for the variable bearing the function's name.

Try it this way:
Static Function test(x As Integer) As Integer
test = stat + x
stat = test
End Function

--
Jay


"Brite" wrote:

I'm not sure if i'm doing something that doesn't work, but why can't i seem
to get a static function to work the same as a static sub.

If i run the following code a few times, and i always use the number 1 as x,
i would think that the function should retain the previous value of "test",
and then just increment it by 1, but this isn't happening. "Test" always
returns as 1. I want this to work as a counter that other subs will pull from.

Static Function test(x As Integer) As Integer
test = test + x
End Function


thanks

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Creating a static function

My mistake - does also apply to procedure declaration

Tim

"Tim Williams" <timjwilliams at gmail dot com wrote in message
...
There's a good example in the VBA Help.
Static applies to variables, not to Functions.

--
Tim Williams
Palo Alto, CA


"Brite" wrote in message
...
I'm not sure if i'm doing something that doesn't work, but why can't i
seem
to get a static function to work the same as a static sub.

If i run the following code a few times, and i always use the number 1 as
x,
i would think that the function should retain the previous value of
"test",
and then just increment it by 1, but this isn't happening. "Test" always
returns as 1. I want this to work as a counter that other subs will pull
from.

Static Function test(x As Integer) As Integer
test = test + x
End Function


thanks





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
static time with if function raghav Excel Discussion (Misc queries) 2 December 4th 08 10:49 PM
Static Now() function sike11 via OfficeKB.com Excel Discussion (Misc queries) 7 April 24th 07 03:47 PM
using now() as static function MINAL ZUNKE New Users to Excel 3 June 29th 05 05:04 PM
static var/function question [email protected] Excel Programming 8 March 15th 05 02:56 AM
Help creating a formula, pulling from a set of static numbers. James Excel Worksheet Functions 1 December 20th 04 06:39 AM


All times are GMT +1. The time now is 04:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"