Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default "Static" Behavior without Static Defn

The variable "sEscRate" is behaving as if it is declared Static in the
following subroutine & function. I'm puzzled why this is. Each invocation of
the function in the sub should be passing it the variable sEscRate with a
value of 0.05. I am expecting each time TestFunc will return 1.05, not the
totalized amount.

Why is it behaving as if it were declared Static? What am I missing here?

Sub TestSub()
Dim sEscRate As Single
Dim i As Integer

sEscRate = 0.05
For i = 1 To 10
Debug.Print TestFunc(sEscRate)
Next i
End Sub


Function TestFunc(EscRate As Single) As Single
EscRate = EscRate + 1
TestFunc = EscRate
End Function



Results Results
Obtained Expected
1.05 0.05
2.05 0.05
3.05 0.05
4.05 0.05
5.05 0.05
6.05 0.05
7.05 0.05
8.05 0.05
9.05 0.05
10.05 0.05


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default "Static" Behavior without Static Defn

D'Oh! I figured it out. It took me a while to remember that parameters
passed to functions are by default passed "ByRef". A simple "ByVal" in the
function solves the problem:

Function TestFunc(ByVal EscRate As Single) As Single

I swear, if I stop programming in VBA for 4 or 5 months, I must forget half
of what I struggled to learn in the previous go-round. This little
brain-fade cost me a couple hours today.

Steve

The variable "sEscRate" is behaving as if it is declared Static in the
following subroutine & function. I'm puzzled why this is. Each invocation of
the function in the sub should be passing it the variable sEscRate with a
value of 0.05. I am expecting each time TestFunc will return 1.05, not the
totalized amount.

Why is it behaving as if it were declared Static? What am I missing here?

Sub TestSub()
Dim sEscRate As Single
Dim i As Integer

sEscRate = 0.05
For i = 1 To 10
Debug.Print TestFunc(sEscRate)
Next i
End Sub


Function TestFunc(EscRate As Single) As Single
EscRate = EscRate + 1
TestFunc = EscRate
End Function



Results Results
Obtained Expected
1.05 0.05
2.05 0.05
3.05 0.05
4.05 0.05
5.05 0.05
6.05 0.05
7.05 0.05
8.05 0.05
9.05 0.05
10.05 0.05




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
how do I make "NOW" function static chineshg Excel Worksheet Functions 2 December 12th 09 02:32 AM
static "now" values bflorox Excel Worksheet Functions 5 October 16th 08 06:29 PM
How to make number of "static" fields stay the same when copying? MrHaugen Excel Discussion (Misc queries) 2 July 11th 06 08:40 PM
sendkeys "^v" behavior changed with the office 2003 version? Tarzan of the VisualBasic Gorillas Excel Programming 0 September 3rd 04 05:02 PM
"Static" Methods in Class Modules Ripan[_3_] Excel Programming 2 January 12th 04 04:55 PM


All times are GMT +1. The time now is 12:41 AM.

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"