Sub calling and passing arguments

J

Jacob

I am trying to call a sub within another sub, i'll refer to the main
sub as sub1 and the sub within that as sub2. I want sub2 to perform a
calculation and find result X. How do I then use X in sub1? why is X
not available to sub1?

thanks,
Jacob
 
N

NickHK

Jacob,
Maybe you should read the replies to your previous post before posting
again.

NickHK
 
C

ct60

Jacob -

By definition, a sub does not return a value. For that use a function.

However, you can get around this by having a sub which fills in the value as
in the following:


Sub sub1()
Dim x As Integer

x = 1

sub2 x

MsgBox x
End Sub

Sub sub2(x As Integer)
x = x+1
End Sub


What value would you expect to be on the messagebox. It is actually 2. You
could achieve the same effect this way also:

Sub sub1()
Dim x As Integer
Dim y As Integer

x = 1

y = func1(x)

MsgBox y
End Sub

Function func1(x As Integer) As Integer
func1 = x + 1
End Function

Hope that clarifies things.

ct60
 

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

Top