Return value in "Sub"

J

Jeff

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

Gary''s Student

You can use a public (global) variable to hold the return value:

Public outputvalue As Integer

Sub mainroutine()
Call ordinate(3)
MsgBox (outputvalue)
End Sub

Sub ordinate(iVal As Integer)
outputvalue = iVal * iVal
End Sub
 
J

Jim Rech

Since arguments are passed ByRef by default:

Sub a()
Dim Argument As Integer
Argument = 1
TestSub Argument
MsgBox Argument
End Sub

Sub TestSub(Arg As Integer)
Arg = Arg * 2
End Sub

--
Jim
| 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.
 
J

JE McGimpsey

???

You can do anything in a Function that you can do in a Sub.

Neither a Sub nor a Function can change formatting, edit ranges, etc. if
called from the context of a worksheet cell.
 
J

JE McGimpsey

One way:

Public Sub TestSub(ByRef Result As Long, ByVal iVal As Integer)
Result = iVal * iVal
End Sub


Public Sub foo()
Dim nResult As Long
Dim iTest As Integer
iTest = 2
TestSub nResult, iTest
MsgBox nResult
End Sub

There's usually no reason to do this, since functions can do anything a
Sub can do.
 
R

Ron Rosenfeld

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Return value for a function. 1
Using a function 2
Drop down list control value in vba 2
Functions 1
VBA - Function Output 2
VBA Cell Addressing 5
VBA word change keys 0
Error passing 2 parameters into a sub 3

Top