View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jon Peltier Jon Peltier is offline
external usenet poster
 
Posts: 6,582
Default Passing variables back to main procedure

Pass both ByRef to the called sub. One has a value, and the other may or may
not. After the called sub runs, the passed variables take on any changed
values.

Sub CallingSub()
Dim PassedValue As Long
Dim ReturnedValue As Long

PassedValue = 1

CalledSub PassedValue, Returned Value

MsgBox ReturnedValue
End Sub

Sub CalledSub(ByRef Passed As Long, ByRef Returned As Long)
Returned = 10 - Passed
End Sub

Often I use called sub as a function, and return true or false depending on
whether there were any errors:

Sub CallingSub()
Dim PassedValue As Long
Dim ReturnedValue As Long
Dim bTest As Boolean

PassedValue = 1

bTest = CalledFunc(PassedValue, ReturnedValue)

MsgBox ReturnedValue
End Sub

Function CalledFunc(ByRef Passed As Long, ByRef Returned As Long) As Boolean
Returned = 10 - Passed
CalledFunc = True
End Function

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Matt McQueen" <Matt wrote in message
...
I've a subroutine that calls another and passes a variable to it. However
within the 'called' sub is a variable that I would like to pass back to
the
main sub. I know that this could be accomplished using module-level
variables, but I'd like to know how to pass a variable 'backwards'.

Cheers.