Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default 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   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF - more than seven arguments Hannah Excel Worksheet Functions 2 March 25th 10 09:21 AM
Arguments Ra Excel Discussion (Misc queries) 2 July 15th 09 06:00 PM
arguments gemini0662 Excel Discussion (Misc queries) 9 July 13th 06 03:48 PM
Arguments... rci Excel Programming 0 March 2nd 04 10:43 PM
Calling macros width arguments in winxp Isidro Vila Verde Excel Programming 2 October 31st 03 03:55 PM


All times are GMT +1. The time now is 01:11 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"