passing variables

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Greetings,

For starters, read these topics in VBA Help.

Understanding Scope and Visibility
Understanding the Lifetime of Variables

A quickie example...

In Module1:
<font face=Courier New><SPAN style="color:#00007F">Dim</SPAN> ModuleOneVariable_Dim <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Public</SPAN> ModuleOneVariable_Public <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br><SPAN style="color:#00007F">Sub</SPAN> Test()<br><SPAN style="color:#00007F">Dim</SPAN> ProcedureLevelVariable <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    ModuleOneVariable_Dim = "Available to Module One"<br>    ModuleOneVariable_Public = "Available to the Project"<br>    ProcedureLevelVariable = _<br>        "Available only to the procedure (Sub/Function)," & _<br>        " but may be passed by argument, either by value or reference."<br>    <br>    <br>    <SPAN style="color:#00007F">Call</SPAN> CalledSubInModuleTwo_TestOne(ProcedureLevelVariable)<br>    MsgBox ProcedureLevelVariable<br>    <SPAN style="color:#00007F">Call</SPAN> CalledSubInModuleTwo_TestOne(, ProcedureLevelVariable)<br>    MsgBox ProcedureLevelVariable<br>    <br>    <br>    <SPAN style="color:#00007F">Call</SPAN> CalledSubInModuleTwoWithoutArgs<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
In Module2:
<font face=Courier New>    <br><SPAN style="color:#00007F">Sub</SPAN> CalledSubInModuleTwo_TestOne(<SPAN style="color:#00007F">Optional</SPAN> <SPAN style="color:#00007F">ByVal</SPAN> VariablePassedByVal, _<br>                         <SPAN style="color:#00007F">Optional</SPAN> <SPAN style="color:#00007F">ByRef</SPAN> VariablePassedByReference)<br>    <br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> IsMissing(VariablePassedByVal) <SPAN style="color:#00007F">Then</SPAN><br>        MsgBox VariablePassedByVal<br>        VariablePassedByVal = "I'm modified, but I'm just a copy, so I cannot be passed back."<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> IsMissing(VariablePassedByReference) <SPAN style="color:#00007F">Then</SPAN><br>        MsgBox VariablePassedByReference<br>        VariablePassedByReference = "I'm modified, and I can be passed back!"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <br><SPAN style="color:#00007F">Sub</SPAN> CalledSubInModuleTwoWithoutArgs()<br>    <br>    MsgBox ModuleOneVariable_Public<br>    <br>    MsgBox ModuleOneVariable_Dim<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

After pasting the code in the two modules, place the cursor in Test() and begin stepping thru (F8 key).
 
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