PDA

View Full Version : Solved: Passing a variable (and its contents) from a sub to a function



k13r4n
12-16-2008, 07:26 PM
Hello everyone

I have got stuck trying to pass some information contained within a variable from the sub in which the information is obtained to a function that will process the information.

Basiclly, i have a sub that runs on a button click on a userform that will take some information from a listbox and assign it to a variable, i then want to call a function and in that, use the information in the variable asigned from the sub.
I dont have any problems with the sub or function they all work, i just need to pass the info from one to the other

Striped down Example below

Sub in the userform

Private Sub ImportAll_Click()
Dim Filename
Filename = ListBox1.List(1)

GetFile 'the function im calling

End Sub


Function in a module

Function GetFile()

DoALoadOfStuff = Filename

End Function


I have googled it and searched the forum and the KB but i either cant find what im after or its way to cryptic for me to understand at my current level of understanding (but im getting better :thumb )

So an Idiots guide would be greatly appreciated.

Cheers


Kieran

Kenneth Hobs
12-16-2008, 08:24 PM
Passing parameters to a Sub and a Function use the same method. Functions return a value as well as performing other tasks.
e.g.

Sub Test()
MsgBox "5+1=" & Add1(5)
End Sub

Function Add1(someNumber As Double) As Double
Add1 = someNumber + 1
End Function

k13r4n
12-16-2008, 08:49 PM
I think i understand what you have shown there, will go give it a go :thumb

Thanks for the quick response :beerchug:


Kieran

k13r4n
12-17-2008, 12:03 AM
yup got it worked out :yay yay

Thanks for your help.

cheers

Kieran