call sub

  • Thread starter Thread starter mike allen
  • Start date Start date
M

mike allen

I am running a large sub( "bottomsub") that I want run "out of sight" b/c it
is bulky, large, etc. that comes up with 2 answers; the last solved with the
first. I want to pull these 2 answers from bottomsub into another sub
("topsub"). I know I could do it by making bottomsub a function, but that
would be just one answer to be pulled into topsub. I tried...

Sub topsub()
a = 4
b = 6
Call bottomsub(a, b)
Range("a1") = c
Range("a2") = d
End Sub

Sub bottomsub(a, b)
c = a + b
d = b - a
End Sub

....but topsub did not "remember" the 2 answers from bottomsub. Any
thoughts? thanks, mike allen
 
Two possiblilities.

1. Declare all varialbes as top-level (i.e. outside of any subroutine
at the top of the code). This makes them public in that module.

Dim a As Long
Dim b As Long
Dim c As Long
Dim d As Long

Sub topsub()
a = 4
b = 6
Call bottomsub(a, b)
Range("a1") = c
Range("a2") = d
End Sub

Sub bottomsub(a, b)
c = a + b
d = b - a
End Sub

2. Pass all 4 variables to the bottom sub.

Sub topsub()
a = 4
b = 6
Call bottomsub(a, b, c, d)
Range("a1") = c
Range("a2") = d
End Sub

Sub bottomsub(a, b, c, d)
c = a + b
d = b - a
End Sub

Note that variables will not be remembered outside their scope. Tha
means that variables declared in one subroutine will not be availabl
in any others even if you use the same names. So you could do this an
it would work too:

Sub topsub()
a = 4
b = 6
Call bottomsub(a, b, c, d)
Range("a1") = c
Range("a2") = d
End Sub

Sub bottomsub(w, x, y, z)
y = w + x
z = x - w
End Sub

In the first example, the variables were delcared at module level, s
they will be available to all routines in the module and cannot b
redeclared in a subroutine.

Hope this explains a bit about variable scope. Do some searchin
either on this board or Google the terms VB Variable Scope to get
better explanation. It becomes more important if using userforms an
passing info from code written in worksheet modules. A good concept t
have a solid grasp of...
 
I think you need to declare your variables as public

At the top of the module above the subs you need to put
Public a,b,c,d

Laura
 
... by making bottomsub a function, but that would be just one answer to
be pulled into topsub.

Not what you asked, but if you would like the bottom function to return two
answers, here are just a couple of ways to incorporate that idea.

Sub TopSub()
Dim a, b, ans
a = 4
b = 6
ans = PlusMinus(b, a)
Range("a1") = ans(0)
Range("a2") = ans(1)
End Sub

Function PlusMinus(a, b)
Dim c, d
c = a + b
d = a - b
PlusMinus = Array(c, d)
End Function

' = = = = = =
Here's another of a few ideas:

Sub TopSub()
Dim a, b, ans
a = 4
b = 6
Range("A1").Resize(2, 1) = PlusMinus(b, a)
End Sub

Function PlusMinus(n, dif)
Dim v(1 To 2, 1 To 1)
v(1, 1) = n + dif
v(2, 1) = n - dif
PlusMinus = v
End Function

' = = = = = =

HTH
 

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