need result from called function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do it get the result variable passed back to the original sub from a
called function?

IE the result of this call function is a variable that is used in the sub

Call EnterSubProjValue

thanks!
 
TMGreen said:
How do it get the result variable passed back to the original sub from a
called function?

Use syntax like this.

Public Sub SubroutineA()
Dim result As Long
Dim passedParameter As Long

passedParameter = 100
result = functionName(passedParameter)
' Do whatever with result.
End Sub

Public Function functionName(nParam As Long) As Long
Dim var As Long

' Do calculation on nParam and put the result in var.
functionName = var
End Function

If there's no parameter to pass, just use empty parentheses after the
function name when calling it and in the definition.
 
Functions return values and Subs do not. If it is a function then:
Me.MyValue = EnterSubProjValue
will assign the returned value.
 
I modified the sub code to

subprojresult = entersubproj()

but I am still not getting a result. in the function I'm setting a value as
follows:
public function
....
subprojvalue = InputBox(message, title, default)
....
end function
the subprojvalue is a global variable and the result is stored at the end of
the function but gone when it comes back to the sum. any clues as to where
I'm going wrong?
 
TMGreen said:
I modified the sub code to

subprojresult = entersubproj()

but I am still not getting a result. in the function I'm setting a
value as follows:
public function
...
subprojvalue = InputBox(message, title, default)
...
end function
the subprojvalue is a global variable and the result is stored at the
end of the function but gone when it comes back to the sum. any
clues as to where I'm going wrong?

To make a function return a value then within the function you need to set the
value of the function itself. If you are instead setting tha value of a global
variable then you need to refer the the golbal variable in youe calling code...

Call entersubproj
subprojresult = GlobalVariableName

....however; returning straight from the function and eliminating the global
variable would be the better way to do it.
 

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

Back
Top