Passing Global Variables by Value

Ed Song

Board Regular
Joined
Sep 1, 2014
Messages
90
I want to create public variables, but always want to pass the current value of the variable from one procedure to another. For Example:

-------------------

Option Explicit

Public X as Long
Public Y as Long
Public Z as Long


Sub A ()

X = 1
Y= 2
Z = X + Y

Call B

Worksheets (1).Range("A1").Value = Z

End Sub

Sub B ()

X = 2
Y=3
Z = X + Y

End Sub

----------------

In Cell A1 I want the value of 5 printed not the value of 3. Thus, I want the variables to be passed by value and not by reference. I want variables to pass by value globally.

Is this an appropriate procedure?

-----------

Option Explicit

Public X ByVal as Long
Public Y ByVal as Long
Public Z ByVal as Long


Sub A ()

X = 1
Y= 2
Z = X + Y

Call B

Worksheets (1).Range("A1").Value = Z

End Sub

Sub B ()

X = 2
Y=3
Z = X + Y

End Sub

----------------
 
Hi,

This gives the sequence of numbers you were looking for:
Code:
Option Explicit

Dim Bob As Long

Sub Main()
    Dim Ann As Long
    Dim Sue As Long
    Ann = 10
    Debug.Print Ann
    
    A
    Debug.Print Bob
    
    B
    Debug.Print Bob
    
    C Bob
    Debug.Print Bob
    
    D Bob, Sue, Ann
    Debug.Print Bob
    Debug.Print Sue
    
    E Bob
    Debug.Print Bob
    
    F (Bob)
    Debug.Print Bob
End Sub

Sub A()
Bob = 1
End Sub

Sub B()
Dim Bob As Long

Bob = 2
End Sub

Sub C(ByRef Joe As Long)
Joe = 3
End Sub

Sub D(ByRef Joe As Long, ByRef Sue As Long, ByRef Ann As Long)
Dim Ann1 As Long
Joe = 4
Ann1 = Ann
Sue = Joe + Ann

End Sub

Sub E(Joe) ' passing ByRef is implicit, data type Variant is implicit
Joe = 5
Stop ' and look at Joe in the Locals window; it's Variant/Long, not Long
End Sub

Sub F(ByRef Joe As Long)
Joe = 6
End Sub

It sounds as if you need to pass all parameters ByRef. That is, there is only one version of the variable and it can be changed either in the main program of by the sub.
ByVal is where the sub takes a copy of the variable and processes that. If you do that then the sub can't pass the value back.

ByRef is the default anyway.

So you could put all your macros in different modules and pass all the variables ByRef.

(By the way, the reason you got nothing from your version of the program is because you added paraneters to the main program. You need to call it from another macro if you do that. It cannot be called directly.)
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Your program looks good. But why when running F, its in the form F (Bob) when Bob is not in parenthesis when running the other subs?

Now comes the tricky part. This is my ultimate question. How do I pass the current value from procedure to procedure in a loop? I now modify your program to:

Option Explicit

Dim Bob As Long

Sub Main()
Dim Ann As Long
Dim Sue As Long
Dim t As Integer
Ann = 10
Debug.Print Ann

t = 1


Do


A
Debug.Print Bob

B
Debug.Print Bob

C Bob
Debug.Print Bob

D Bob, Sue, Ann
Debug.Print Bob
Debug.Print Sue

E Bob
Debug.Print Bob

F (Bob)
Debug.Print Bob
End Sub

t = t + 1
Loop Until t = 4

Sub A()
Bob = 1
End Sub

Sub B()
Dim Bob As Long

Bob = 2
End Sub

Sub C(ByRef Joe As Long)
Joe = 3
End Sub

Sub D(ByRef Joe As Long, ByRef Sue As Long, ByRef Ann As Long, ByRef t as Integer)
Dim Ann1 As Long
Joe = t + 3
Ann1 = Ann
Sue = Joe + Ann

End Sub

Sub E(Joe) ' passing ByRef is implicit, data type Variant is implicit
Joe = 5
Stop ' and look at Joe in the Locals window; it's Variant/Long, not Long
End Sub

Sub F(ByRef Joe As Long)
Joe = 6
End Sub

The goal is to add one to Sue and Bob in procedures D and E each time we go through the loop, or add the value of t to the initial values of Sue and Bob. The problem is that I can't pass the value of t from the main program to the subroutines. The current problem I'm working on, must pass many current variable values from the main to the subroutines because its an iteration program. So the question is how can I structure the program to accomplish my goals? This is why in an earlier post I asked whether my program has to be done in one subroutine. The current program above does not quite accomplish my goal.
 
Last edited:
Upvote 0
Putting the argument in parens when the parens are not required causes an evaluation of the argument, which effectively means that it is passed by value, despite that the Sub's request (in the signature) that it be passed by reference.
 
Upvote 0
I've got the general form I want working. This is a simplified program that is in the form of my program:


Option Explicit


Sub main()
Dim x As Long
Dim y As Long
Dim z As Long
Dim t As Integer


t = 1


Do
If t = 1 Then
x = 2
Else
x = 3
End If
Debug.Print x

Do Until x > 100


Call A(y, z)
x = x + y + z
Debug.Print x

Call B(y, z)
x = x + y + z
Debug.Print x
Loop
t = t + 1
Loop Until t = 3
Debug.Print x

End Sub


Sub A(y1 As Long, z1 As Long)
y1 = 1
z1 = 4


End Sub


Sub B(y2 As Long, Z2 As Long)
y2 = 2
Z2 = 4





End Sub


The output obtained is:


2,7,13,18,24,29,35,40,46,51,57,62,68,73,79,84,90,95,101,3,8,14,19,25,30,36,41,47,52,58,63,69,74,80.80,91,96,102,102.


This is the exact output I want.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top