Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Passing variables back to main procedure

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default Passing variables back to main procedure

Matt,

try passing it by reference to the called sub.. For example:

Sub Main()
Dim i As Integer
i = 1
Increment i
Debug.Print i
End Sub

Sub Increment(ByRef x As Integer)
x = x + 1
End Sub



--
Hope that helps.

Vergel Adriano


"Matt McQueen" wrote:

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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
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.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Passing variables back to main procedure

You can't really do "pass backward" as such, but there are two alternative
methods you can use. The first is to make the called procedure a Function
not a Sub. A Function procedure can return a value to its caller. E.g.,

Sub Calling()
Dim Result As Long
Result = Called()
Debug.Print Result
End Sub

Function Called() As Long
' some code
Called = 1234
End Sub

The second method is to pass ByRef an argument to the Called procedure and
have the Called procedure set that value.

Sub Calling()
Dim L As Long
Called L
Debug.Print L
End Sub

Sub Called(ByRef LL As Long)
' some code
LL = 1234
End Sub

Because the parameter LL is declared ByRef the variable L in the Calling
procedure gets the value 1234. ByRef is the default method of passing
parameters so you don't necessarily need the ByRef keyword, but I usually
include it to emphasize that the parameter is going to be set by the called
procedure. The name of the parameter (LL in the example) need not have the
same name as the variable in the Calling procedure (L in the example).


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)


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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Passing variables back to main procedure

Cheers gents - I hadn't realised that passed variables took on the values
determined in the called sub.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Passing variables back to main procedure

They do when passed ByRef, not when passed ByVal. Of course, ByRef is the
default....

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


"Matt McQueen" wrote in message
...
Cheers gents - I hadn't realised that passed variables took on the values
determined in the called sub.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to set a userform's button caption equal to a variable in the main procedure? [email protected] Excel Programming 1 August 11th 06 09:19 PM
How to elegantly end a main procedure from a userform command button? [email protected] Excel Programming 1 August 2nd 06 11:54 PM
How to get get variables to move from userform to main sub? Chet Excel Programming 1 May 10th 06 11:14 PM
Pass switch to Main procedure Bob Phillips[_6_] Excel Programming 0 August 3rd 05 11:23 PM
Passing CheckBox Value from Click Event to Main Macro ExcelMonkey[_190_] Excel Programming 2 February 1st 05 05:47 PM


All times are GMT +1. The time now is 01:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"