what happens to code after call method in Sub?

dantheman9

Board Regular
Joined
Feb 5, 2011
Messages
175
Hi,

Im running a sub which then calls another Sub, does vba return to that point in the first sub after running the second Sub? (I'm looking to set a variable from the second sub code for use in the first).


thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi,

Im running a sub which then calls another Sub, does vba return to that point in the first sub after running the second Sub? (I'm looking to set a variable from the second sub code for use in the first).


thanks
Yes it returns to sub1 after sub2 runs through completely. If you want to use a variable in multiple subs I would use a global variable that you would put outside of all subs. For example,

Global x as long

Sub1()
Call Sub2()
End Sub

Sub2()
blah blah
End Sub
 
Upvote 0
Yes. Except for error handling, all procedures return to the line following the invocation (e.g., the Call statement) in the calling routine.
 
Upvote 0
Yes- as you say - here it passes a variable abc set in seconsub back to firstsub. Being it is Private sub - it does not show on macro list.


Code:
Option Explicit
Public Sub Firstsub()
    Dim abc As Single
    Call SecondSub(abc)
End Sub
Private Sub SecondSub(abc)
    abc = 4
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top