ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   ByRef vs ByVal (https://www.excelbanter.com/excel-programming/389263-byref-vs-byval.html)

Grd

ByRef vs ByVal
 
Hi there,

I have the following code (its simplified). I want to pass a value to a
routine and for it to be doubled. I thought by using ByRef it would come back
to the oriingal routine changed but I must be missing something. I have an
example below. I get 100 instead of 200 at the end of the Test procedure.

What am I doing Wrong.

Any help greatly appreciated.

Tx

Suzanne

Sub Test
Dim y
y =100
Call Test2(y)
Debug.Print y
End Sub

Sub Test2( ByRef x)
x = x *2
End Sub



Dave Peterson

ByRef vs ByVal
 
I got 200 in the immediate window.

You may want to copy|paste your exact code--instead of typing the code in the
post.

Grd wrote:

Hi there,

I have the following code (its simplified). I want to pass a value to a
routine and for it to be doubled. I thought by using ByRef it would come back
to the oriingal routine changed but I must be missing something. I have an
example below. I get 100 instead of 200 at the end of the Test procedure.

What am I doing Wrong.

Any help greatly appreciated.

Tx

Suzanne

Sub Test
Dim y
y =100
Call Test2(y)
Debug.Print y
End Sub

Sub Test2( ByRef x)
x = x *2
End Sub


--

Dave Peterson

Grd

ByRef vs ByVal
 
Thanks Dave

So do I now. I must have retyped it and somehow fixed it.
Hooray

Tx again
S

"Dave Peterson" wrote:

I got 200 in the immediate window.

You may want to copy|paste your exact code--instead of typing the code in the
post.

Grd wrote:

Hi there,

I have the following code (its simplified). I want to pass a value to a
routine and for it to be doubled. I thought by using ByRef it would come back
to the oriingal routine changed but I must be missing something. I have an
example below. I get 100 instead of 200 at the end of the Test procedure.

What am I doing Wrong.

Any help greatly appreciated.

Tx

Suzanne

Sub Test
Dim y
y =100
Call Test2(y)
Debug.Print y
End Sub

Sub Test2( ByRef x)
x = x *2
End Sub


--

Dave Peterson



All times are GMT +1. The time now is 02:58 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com