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

How do you pass parameters from a calling sub to a function either by 1) reference or by 2)value
By reference I mean: when the value is changed in the called function it is also changed in the calling sub (once program flow resumes in the sub
By value I mean: when the value is changed in the called function it is not changed in the calling sub (the function returns it to the calling function.) This brings me to another question: I've tried using the Return statement in VBA and it does not recognize it. Is there another statement to return a value

Thanks
Warren R

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Passing parameters

This example shows the difference between ByVal and ByRef.
Change the ByRef below to ByVal to see the difference.

Note that if you don't specify ByVal or ByRef, the default is ByRef

Sub test()
Dim strParameter As String, strResult As String

strParameter = "My Parameter Text"
strResult = DoStuff(strParameter)

MsgBox "strResult = " & strResult
MsgBox "strParameter = " & strParameter
End Sub

Function DoStuff(ByRef strPassedParameter As String) As String
strPassedParameter = "Altered Text"
DoStuff = "Returning Stuff"
End Function


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"WarrenR" wrote in message
...
How do you pass parameters from a calling sub to a function either by 1)

reference or by 2)value.
By reference I mean: when the value is changed in the called function it

is also changed in the calling sub (once program flow resumes in the sub.
By value I mean: when the value is changed in the called function it is

not changed in the calling sub (the function returns it to the calling
function.) This brings me to another question: I've tried using the Return
statement in VBA and it does not recognize it. Is there another statement
to return a value?

Thanks,
Warren R.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Passing parameters

Just thought of another related question... For the called routine(in the example below, it's a function), is there a general rule as to when this should be set up as a sub rather than a function? I'm still grasping the differnce between the two
Thanks
Warren R

----- Rob van Gelder wrote: ----

This example shows the difference between ByVal and ByRef
Change the ByRef below to ByVal to see the difference

Note that if you don't specify ByVal or ByRef, the default is ByRe

Sub test(
Dim strParameter As String, strResult As Strin

strParameter = "My Parameter Text
strResult = DoStuff(strParameter

MsgBox "strResult = " & strResul
MsgBox "strParameter = " & strParamete
End Su

Function DoStuff(ByRef strPassedParameter As String) As Strin
strPassedParameter = "Altered Text
DoStuff = "Returning Stuff
End Functio


--
Rob van Gelder - http://www.vangelder.co.nz/exce


"WarrenR" wrote in messag
..
How do you pass parameters from a calling sub to a function either by 1

reference or by 2)value
By reference I mean: when the value is changed in the called function i

is also changed in the calling sub (once program flow resumes in the sub
By value I mean: when the value is changed in the called function it i

not changed in the calling sub (the function returns it to the callin
function.) This brings me to another question: I've tried using the Retur
statement in VBA and it does not recognize it. Is there another statemen
to return a value
Thanks

Warren R

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Passing parameters

For calling from code, the only difference is whether the procedure
returns a result or not: Subs don't; Functions, do.

Only Functions can be called from a worksheet.

In article ,
"WarrenR" wrote:

Just thought of another related question... For the called routine(in the
example below, it's a function), is there a general rule as to when this
should be set up as a sub rather than a function? I'm still grasping the
differnce between the two.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Passing parameters

Perfect! Thank you JE
-Warren

----- JE McGimpsey wrote: ----

For calling from code, the only difference is whether the procedure
returns a result or not: Subs don't; Functions, do

Only Functions can be called from a worksheet

In article
"WarrenR" wrote

Just thought of another related question... For the called routine(in the
example below, it's a function), is there a general rule as to when this
should be set up as a sub rather than a function? I'm still grasping the
differnce between the two


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
Passing parameters Cel Ref) to Sum function Shamshad Butt Excel Discussion (Misc queries) 4 October 26th 05 10:46 AM
Passing parameters between functions Gaston[_7_] Excel Programming 4 February 9th 04 04:08 AM
Passing parameters Eleanor[_2_] Excel Programming 0 February 4th 04 05:21 PM
Passing parameters to UDF mbobro[_3_] Excel Programming 5 January 2nd 04 04:25 PM
Passing Parameters through OnAction Mark Bigelow Excel Programming 3 September 10th 03 12:53 AM


All times are GMT +1. The time now is 04:00 PM.

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

About Us

"It's about Microsoft Excel"