Article ID: 140033
Article Last Modified on 10/11/2006
Sub PassVarValues()
' Declare variables.
Dim PassVar1 as Integer
Dim PassVar2 as Integer
' Set the variable PassVar1 to equal 1234.
PassVar1 = 1234
' Set the variable PassVar2 to equal 5678.
PassVar2 = 5678
' Run the macro Receiver and pass the variables to the subroutine.
' On a Macintosh computer, you may need to omit the .xls file
' extension.
Application.Run "Book2.xls!Receiver", PassVar1, PassVar2
End Sub
Sub Receiver(PassVar1 As Integer, PassVar2 As Integer)
' Declare variable.
Dim Result1 as Integer
' Manipulate the variables.
Result1 = PassVar1 + PassVar2
' Displays the value of the variable PassVar1 in a message box.
MsgBox PassVar1
' Displays the value of the variable PassVar2 in a message box.
MsgBox PassVar2
' Displays the value of the variable Result1 in a message box.
MsgBox Result1
End Sub
Sub PassVarValues()
' Declare variables.
Dim PassVar1 as Integer
Dim PassVar2 as Integer
' Set the variable PassVar1 to equal 1234.
PassVar1 = 1234
' Set the variable PassVar2 to equal 5678.
PassVar2 = 5678
' Run the macro Receiver and pass the variables to the
' subroutine.
' On a Macintosh computer, you may need to omit the .xls file
' extension.
Application.Run "Book2.xls!Receiver", PassVar1, PassVar2
' The following line is new:
ActiveWorkbook.Close ' Closes the workbook Book1.xls.
End Sub
' Set the passed variables to Public so MacroDisp macro can
' display the values passed to Receiver
Public NewVar1, NewVar2, Result2
Sub Receiver(PassVar1 As Integer, PassVar2 As Integer)
' Declare variable.
Dim Result1 as Integer
' The following 2 code lines are new:
' Set NewVar1 equal to PassVar1, so we can retain the passed
' variable value.
NewVar1 = PassVar1
' Set NewVar2 equal to PassVar2, so we can retain the passed
' variable value.
NewVar2 = PassVar2
' The variables in the following four code lines have been
' changed:
' Manipulate the variables.
Result2 = NewVar1 + NewVar2
' Displays the value of the variable NewVar1 in a message box.
MsgBox NewVar1
' Displays the value of the variable NewVar2 in a message box.
MsgBox NewVar2
' Displays the value of the variable Result2 in a message box.
MsgBox Result2
End Sub
Sub MacroDisp()
' Displays the value of variable NewVar1 in a message box.
MsgBox NewVar1
' Displays the value of variable NewVar2 in a message box.
MsgBox NewVar2
' Displays the value of variable Result2 in a message box.
MsgBox Result2
End Sub
Sub PassVar2()
' Declare variable.
Dim PassVar1 as Integer
' Set the variable PassVar1 to equal 14.
PassVar1 = 14
' Run the macro RecVar2 in Module2 and pass it the variable
' PassVar1.
Module2.RecVar2(PassVar1)
End Sub
Sub RecVar2(PassVar1)
' Display the value of variable PassVar1 in a message box.
MsgBox PassVar1
End Sub
' Set the variable PublicVar1 public, so module2 will have access to
' it.
Public PublicVar1
Sub PublicVar()
' Set the variable PublicVar1 to equal 12.
PublicVar1 = 12
' Run the macro RecVar on Module2 macro sheet.
Module2.RecVar
End Sub
Sub RecVar()
' Display the value of variable PublicVar1 in a message box
MsgBox PublicVar1
End Sub
Additional query words: 5.00a 5.00c XL
Keywords: kbdtacode kbhowto kbprogramming KB140033