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).