View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.misc
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Return value in "Sub"

On Wed, 27 Feb 2008 04:53:00 -0800, Jeff
wrote:

Hi,

I was trying to figure out how to return a value from a "Sub" procedure. I
understand that if you write a function you can set a value equal to that
function and that will return the value. For example

Public Function TestFunction(iVal as integer)
(
TestFunction = iVal * iVal
)

Var = TestFunction(4) ' will return 4 * 4 to the variable "Var"

But in a Sub

Sub TestSub(iVal as integer)
(
TestSub = iVal * iVal
)

Call TestSub

Does not work. I dont want to use functions because I cannot do as much as
in a sub and this is just a simple example, I forget the rules but I dont
think you can edit ranges or edit objects.


Here's a routine with an example. The variable sent to the Sub by foo() will
be modified by that Sub, and can then be used in foo():

========================
Option Explicit

Sub TestSub(iVal As Integer)
iVal = iVal * iVal
End Sub

Sub foo()
Dim TestNo As Integer
TestNo = 23
TestSub TestNo
Debug.Print TestNo
End Sub
============================
--ron