Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
???
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
function to return day in the form "Monday", "Tuesday" etc given . | Excel Worksheet Functions | |||
Check if cells contain the word "Thailand", return "TRUE" | Excel Worksheet Functions | |||
change "true" and "false" to "availble" and "out of stock" | Excel Worksheet Functions | |||
If (A1 = "ValueA" & C1 = "ValueB") return the quantity of matches | Excel Discussion (Misc queries) | |||
Count occurences of "1"/"0" (or"TRUE"/"FALSE") in a row w. conditions in the next | New Users to Excel |