Protecting values of variables

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top