Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Protecting values of variables

Hi all! I need to know of a way to protect the values of variables as they
are passed between function to function. I didn't notice this at first, but
it explains why my final answer was getting a number not physically posible.
Here's some code to help explain...

Sub Macro2()
a = 1
b = 2
c = 3
dummy = Ben(a, b, c)
End Sub

Function Ben(x, y, z)
x = y + z
Ben = x
End Function
Note, that at the "end sub" line, the value of "a" is now 5 instead of 2...
thanks for your help!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default Protecting values of variables

Use byVal in the function. I believe the default passing mechanism is
byRef.
When you use byVal, the value is copied to working storage separate from
that used in Macro2(), and so should protect Macro2()'s working storage.
Function Ben(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As
Integer

"Ben H" wrote in message
...
Hi all! I need to know of a way to protect the values of variables as

they
are passed between function to function. I didn't notice this at first,

but
it explains why my final answer was getting a number not physically

posible.
Here's some code to help explain...

Sub Macro2()
a = 1
b = 2
c = 3
dummy = Ben(a, b, c)
End Sub

Function Ben(x, y, z)
x = y + z
Ben = x
End Function
Note, that at the "end sub" line, the value of "a" is now 5 instead of

2...
thanks for your help!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 400
Default Protecting values of variables

Unless you specify ByVal, the default ByRef is assumed & changes stick. See
below:
Function Ben(byval x, byval y, byval z)
x = y + z
Ben = x
End Function

"Ben H" wrote:

Hi all! I need to know of a way to protect the values of variables as they
are passed between function to function. I didn't notice this at first, but
it explains why my final answer was getting a number not physically posible.
Here's some code to help explain...

Sub Macro2()
a = 1
b = 2
c = 3
dummy = Ben(a, b, c)
End Sub

Function Ben(x, y, z)
x = y + z
Ben = x
End Function
Note, that at the "end sub" line, the value of "a" is now 5 instead of 2...
thanks for your help!

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
Split values in cells to variables [email protected] Excel Discussion (Misc queries) 4 January 15th 08 02:41 AM
VBA variables retaining their values GeorgeJ Excel Discussion (Misc queries) 2 August 17th 07 02:19 AM
how to look up values with two independent variables charlesfung Excel Worksheet Functions 4 June 17th 06 03:32 PM
Changing the values of variables mickiedevries[_6_] Excel Programming 0 June 23rd 04 07:48 PM
can variables pass values Don[_11_] Excel Programming 3 November 1st 03 04:50 PM


All times are GMT +1. The time now is 10:30 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"