Sub calling and passnig 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
 
G

Guest

Refer to VBA help for Scope and Visibility. You'll need to declare your
variable as module level or public instead of procedure level (X is declared
outside of Sub1, not inside of Sub1).

Since Sub2 performs a calculation and returns a value, you could also make
Sub2 a function. Just set Sub2 = whatever value you want it to return before
Sub2 terminates.

A simple example:

Sub Sub1()
msgbox Sub2(3, 5)
End Sub

Function Sub2 (Arg1, Arg2)
Sub2 = Arg1 + Arg2
End Function
 
G

Guest

Sub sub1()
Dim y as double, myX as Double
y = 31
myX = Sub2(y)
msgbox y & " squared is " & myX
end sub

function Sub2(yy as double) as Double
xx = yy^2
Sub2 = xx
End Function

Look up Scope in VBA help.
 

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