Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Sub calling and passnig arguments
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Sub calling and passnig arguments
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 "Jacob" wrote: 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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Sub calling and passnig arguments
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. -- Regards, Tom Ogilvy "Jacob" wrote: 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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
IF - more than seven arguments | Excel Worksheet Functions | |||
Arguments | Excel Discussion (Misc queries) | |||
arguments | Excel Discussion (Misc queries) | |||
Arguments... | Excel Programming | |||
Calling macros width arguments in winxp | Excel Programming |