ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sub calling and passing arguments (https://www.excelbanter.com/excel-programming/373918-sub-calling-passing-arguments.html)

Jacob

Sub calling and passing 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


NickHK[_3_]

Sub calling and passing arguments
 
Jacob,
Maybe you should read the replies to your previous post before posting
again.

NickHK

"Jacob"
egroups.com...
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




ct60

Sub calling and passing arguments
 
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



"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




All times are GMT +1. The time now is 02:57 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com