Carrying over defined values from one sub to another

kc_native

Board Regular
Joined
Jan 20, 2009
Messages
176
Okay, the following is going to sound incredibly naive, but as I have said before, I am learning this stuff as I go along. I am defining values in a sub that I want to carry over to other subs that I call in sequence, but as I am finding out, the values go to "Empty" when the new sub runs. How can I retain the values (below) in the original sub, without having to re-define them all over again for each sub? Am using the following to define the values:
Code:
Report_as_of_date = InputBox("What is the date that you pulled the IFMS _
Forecast FTE data from MicroStrategy?. Example: 03/28/11", "DATE?")
 
Next_FY = InputBox("What is the next fiscal year?. Example: 2012", "NEXT _
FISCAL YEAR?", "2012")
 
Current_FY = Next_FY - 1
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Either declare your variables at the top of your module outside any procedures, or pass them to the subsequent procedures directly as arguments. For example:
Code:
sub macro1()
dim as_of_date as date
as_of_date = dateserial(2011,1,1)
call macro2(as_of_date)
end sub
 
sub macro2(dte as Date)
  msgbox dte
End Sub
 
Upvote 0
Either declare your variables at the top of your module outside any procedures, or pass them to the subsequent procedures directly as arguments. For example:
Code:
sub macro1()
dim as_of_date as date
as_of_date = dateserial(2011,1,1)
call macro2(as_of_date)
end sub
 
sub macro2(dte as Date)
  msgbox dte
End Sub
So, in my case, if I wanted to pass two variables as values that will not change in the following subs, would I do the following?
Code:
Sub FTE_Forecast_Conversion()     ' my first sub
 
 'procedure'
 
Call Senior(ByVal Report_as_of_date as Date, Next_FY as Integer)
End Sub
 
Sub Senior()      'my second sub
 
'Procedure'
 
Call Principal(ByVal Report_as_of_date as Date, Next_FY as Integer)
End Sub
 
Sub Prinicpal()      'my third sub
 
And so on......
 
Upvote 0
Sort of, except you have to declare the other subs to take arguments, and you just pass the values to them:
Code:
Sub FTE_Forecast_Conversion()     ' my first sub    'procedure'
 Call Senior(Report_as_of_date, Next_FY)
End Sub
Sub Senior(dteIn as Date, intIn as integer)

    Call Principal(dtein, intIn)
End Sub

Sub Principal(somedate as date, someint as integer)
'...
End Sub
 
Upvote 0
Sort of, except you have to declare the other subs to take arguments, and you just pass the values to them:
Code:
Sub FTE_Forecast_Conversion()     ' my first sub    'procedure'
 Call Senior(Report_as_of_date, Next_FY)
End Sub
Sub Senior(dteIn as Date, intIn as integer)
 
    Call Principal(dtein, intIn)
End Sub
 
Sub Principal(somedate as date, someint as integer)
'...
End Sub
So I notice you are calling the variables something different in each successive sub. Does that mean I can't pass the same variable names forward, and just reference them in the procedure for the next sub?
 
Upvote 0
You can use the same variable names if you wish - I specifically changed them to demonstrate that there is no inherent link between the names in different procedures. In other words each procedure refers to the variable by the name in its own declaration, not by the name of the variable passed to it by the calling procedure.
 
Upvote 0
You can use the same variable names if you wish - I specifically changed them to demonstrate that there is no inherent link between the names in different procedures. In other words each procedure refers to the variable by the name in its own declaration, not by the name of the variable passed to it by the calling procedure.

I got it to work using the same variable names in the next sub. Woohoo!!! Thanks a million royra! You have tought me a powerful new tool.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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