Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables to functions

Status
Not open for further replies.

EddyLLC

Technical User
Mar 15, 2005
304
US
I know it is possible to pass variables to a function but is it possible to pass them back to the original function. In the first function I am creating one long string of data. I need to pass that string of data to a function that adds to the string then pass the modified string back to the original function for further changes.
Is this possible? I apologize if this post is clear as mud, it's late.

Thanks
 
Do you mean something like:
Code:
Sub SendForChange()
    Debug.Print ChangeThis("ABCD")
End Sub
Function ChangeThis(strString)
    ChangeThis = strString & "EFGH"
End Function
 
Have a look at ByRef in the VBA help.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Is this what you mean? A recursive function, is a function that calls itself.

Code:
Public Function callMySelf(ByVal myInt As Integer) As Integer
  myInt = myInt + 1
  If myInt = 10 Then Exit Function
  callMySelf = myInt
  Debug.Print callMySelf(myInt)
End Function
Output:

?callMySelf(0)
0
9
8
7
6
5
4
3
2
1
 
Thanks for the responses. Here is what I have:

Private Sub First()
Dim strWriteLine as String
Second(strWriteLine)
End Sub

Private Sub Second(strWriteLine As String)
strWriteLine = strWriteLine & "Other Stuff"
End Sub

The sub First creates the variable strWriteLine and adds data to it. I then call the sub Second passing the variable strWriteLine. This works fine. When I stop the code and check the variable it is passed intact. The sub second adds "Other Stuff" to the variable correctly. The problem is when sub Second ends an I am returned to Sub First, strWriteLine is back to its original data as if it was never passed to sub Second. The "Other Stuff" is no longer there.
How can I cause the strWriteLine to retain the "Other Stuff" added to it in sub Second after returning to sub First?

Thanks.
 
Replace this:
Second(strWriteLine)
with either this:
Call Second(strWriteLine)
or this:
Second strWriteLine

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You nailed it PHV. Such a simple solution. Thanks so much for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top