![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: May 2002
Location: Auckland
Posts: 412
|
Hello,
I am trying to pass arguments/variables between sub routines/functions, this I can do. What I am having trouble doing is returning a value to the calling sub that did not pass a variable to begin with. Specifically I am asking some one to select a file to attach to an email. The email sub calls the open email sub, what I need is the file name returned as a variable to the calling email sub. One approach I have used successfully is, setting a global variable to be used by all routines. This I feel can get messy, and would like to know how to pass/return between subs. I hope you can help me. |
|
|
|
|
|
#2 |
|
Board Regular
Join Date: Apr 2002
Posts: 112
|
Subs cannot return a value. Functions can return a value. Try changing the Sub rountine into a Function.
|
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
|
Hi jag108...
Same answer as above. Just a bit more details... In the following code example, running CallingSub... MyAttachment = GetFileName Your function will run and return the string "C:A File" to MyAttachment
You can also pass parameters to your functions just like Subs. In the following example, MyAttachment = "C:B File"
Tom [ This Message was edited by: TsTom on 2002-05-15 16:41 ] |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
Subs can have parameters tough ! so, if you don't want to have a public variable (I DO like to have them !) use something like
Sub Test1() Dim a As Integer a = 1 Test2 a MsgBox a End Sub Sub Test2(ByRef a As Integer) a = a * 2 End Sub So, it's not entirely true that Subs cannot return a value... if you define them "ByRef"... |
|
|
|
|
|
#5 |
|
Board Regular
Join Date: May 2002
Location: Auckland
Posts: 412
|
Thanks to every one for the great advise regarding my problem.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|