Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 921
Default Return value in "Sub"

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.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Return value in "Sub"

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

--
Gary''s Student - gsnu200770


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

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 533
Default Return value in "Sub"

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
"Jeff" wrote in message
...
| 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.


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,624
Default Return value in "Sub"

???

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.


In article ,
Jeff wrote:

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.

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,624
Default Return value in "Sub"

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.


In article ,
Jeff wrote:

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



  #6   Report Post  
Posted to microsoft.public.excel.misc
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
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
function to return day in the form "Monday", "Tuesday" etc given . MTro Excel Worksheet Functions 2 October 3rd 07 09:49 AM
Check if cells contain the word "Thailand", return "TRUE" ali Excel Worksheet Functions 7 September 14th 07 09:53 AM
change "true" and "false" to "availble" and "out of stock" inthestands Excel Worksheet Functions 2 July 19th 07 07:05 PM
If (A1 = "ValueA" & C1 = "ValueB") return the quantity of matches DaveC Excel Discussion (Misc queries) 2 July 1st 07 10:34 AM
Count occurences of "1"/"0" (or"TRUE"/"FALSE") in a row w. conditions in the next BCB New Users to Excel 7 May 13th 06 10:02 PM


All times are GMT +1. The time now is 01:55 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"