Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 295
Default Passing a value to another macro

How do I pass a value from one macro to another? For example (simplified code)

sub aa()
mynum = 12
Application.Run "bb" ' pass the value of mynum to "bb"
' get the value of myvalue back from "bb"
'do some other stuff in "aa"
end Sub
----------------------------
Sub bb()
counter = 1
do while counter <= mynum
' code to execute in loop
counter = counter + 1
loop
myvalue = 66 ' pass the value of myvalue back to "aa"
end sub

Any help much appreciated, thanks, Brett


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Passing a value to another macro

On Nov 6, 10:28 pm, Brettjg wrote:
How do I pass a value from one macro to another? For example (simplified code)

sub aa()
mynum = 12
Application.Run "bb" ' pass the value of mynum to "bb"
' get the value of myvalue back from "bb"
'do some other stuff in "aa"
end Sub
----------------------------
Sub bb()
counter = 1
do while counter <= mynum
' code to execute in loop
counter = counter + 1
loop
myvalue = 66 ' pass the value of myvalue back to "aa"
end sub

Any help much appreciated, thanks, Brett



You need to do three things:
1. Change Sub bb() to Function bb(mynum). "End Sub" will
automatically change to "End Function".
2. At the end of Function bb, just before "End Function", insert the
statement bb = myvalue. This statement assigns a value that the
function will return to the caller upon completion of the called
function.
3. From within Sub aa, instead of calling Application.Run "bb", use
this: myvalue = bb(mynum). That will call bb and pass mynum as the
argument to it, and at the same time return a value and assign it to
myvalue.

Having said that... Now, a word to the wise: you are strongly
discouraged from naming input arguments the same as function
parameters. To the computer it makes no difference here, but it
creates chaos and confusion for you a year from now when you revisit
your code, as well as for anyone else who will try to read it. In
other words, "mynum" is Sub aa and "mynum" in Function bb do not need
to have the same names, nor should they be named the same. For
example, you could change mynum to inputNum within Function bb, and
things would still work just as smoothly (and the rest of us will
thank you!).

All the above will still work even if Sub aa is in one module, and
Function bb is in another within a VBA project, as long as they are
both Public (which is true in your example). I humbly suggest reading
up Help on Functions and Subs for more information.

Cheers,
-Basilisk96

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Passing a value to another macro

Sorry for my typo:
"mynum" is Sub aa and "mynum" in Function bb do not need <<<


that should be:
"mynum" in Sub aa and "mynum" in Function bb do not need <<<


;-)

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 295
Default Passing a value to another macro

Thankyou very much, I'll put that in and run it, Regards Brett

"Basilisk96" wrote:

On Nov 6, 10:28 pm, Brettjg wrote:
How do I pass a value from one macro to another? For example (simplified code)

sub aa()
mynum = 12
Application.Run "bb" ' pass the value of mynum to "bb"
' get the value of myvalue back from "bb"
'do some other stuff in "aa"
end Sub
----------------------------
Sub bb()
counter = 1
do while counter <= mynum
' code to execute in loop
counter = counter + 1
loop
myvalue = 66 ' pass the value of myvalue back to "aa"
end sub

Any help much appreciated, thanks, Brett



You need to do three things:
1. Change Sub bb() to Function bb(mynum). "End Sub" will
automatically change to "End Function".
2. At the end of Function bb, just before "End Function", insert the
statement bb = myvalue. This statement assigns a value that the
function will return to the caller upon completion of the called
function.
3. From within Sub aa, instead of calling Application.Run "bb", use
this: myvalue = bb(mynum). That will call bb and pass mynum as the
argument to it, and at the same time return a value and assign it to
myvalue.

Having said that... Now, a word to the wise: you are strongly
discouraged from naming input arguments the same as function
parameters. To the computer it makes no difference here, but it
creates chaos and confusion for you a year from now when you revisit
your code, as well as for anyone else who will try to read it. In
other words, "mynum" is Sub aa and "mynum" in Function bb do not need
to have the same names, nor should they be named the same. For
example, you could change mynum to inputNum within Function bb, and
things would still work just as smoothly (and the rest of us will
thank you!).

All the above will still work even if Sub aa is in one module, and
Function bb is in another within a VBA project, as long as they are
both Public (which is true in your example). I humbly suggest reading
up Help on Functions and Subs for more information.

Cheers,
-Basilisk96


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 295
Default Passing a value to another macro

Hi Basil, well, it worked first time! The code is of course much more
complicated than I had submitted on here. I was previously doing the same
thing with a series of GOTO statements, which I hate doing because it creates
spagetti. Thanks again.

"Basilisk96" wrote:

On Nov 6, 10:28 pm, Brettjg wrote:
How do I pass a value from one macro to another? For example (simplified code)

sub aa()
mynum = 12
Application.Run "bb" ' pass the value of mynum to "bb"
' get the value of myvalue back from "bb"
'do some other stuff in "aa"
end Sub
----------------------------
Sub bb()
counter = 1
do while counter <= mynum
' code to execute in loop
counter = counter + 1
loop
myvalue = 66 ' pass the value of myvalue back to "aa"
end sub

Any help much appreciated, thanks, Brett



You need to do three things:
1. Change Sub bb() to Function bb(mynum). "End Sub" will
automatically change to "End Function".
2. At the end of Function bb, just before "End Function", insert the
statement bb = myvalue. This statement assigns a value that the
function will return to the caller upon completion of the called
function.
3. From within Sub aa, instead of calling Application.Run "bb", use
this: myvalue = bb(mynum). That will call bb and pass mynum as the
argument to it, and at the same time return a value and assign it to
myvalue.

Having said that... Now, a word to the wise: you are strongly
discouraged from naming input arguments the same as function
parameters. To the computer it makes no difference here, but it
creates chaos and confusion for you a year from now when you revisit
your code, as well as for anyone else who will try to read it. In
other words, "mynum" is Sub aa and "mynum" in Function bb do not need
to have the same names, nor should they be named the same. For
example, you could change mynum to inputNum within Function bb, and
things would still work just as smoothly (and the rest of us will
thank you!).

All the above will still work even if Sub aa is in one module, and
Function bb is in another within a VBA project, as long as they are
both Public (which is true in your example). I humbly suggest reading
up Help on Functions and Subs for more information.

Cheers,
-Basilisk96




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 arguments from an excel macro to a word macro KWE39 Excel Discussion (Misc queries) 1 July 7th 05 03:56 PM
Passing Variables to another macro Jeff Excel Programming 3 January 16th 05 03:41 AM
Passing variables from Outlook Macro to Excel Macro [email protected] Excel Programming 2 January 7th 05 10:07 PM
Passing Solver a Macro TarrynG Excel Programming 0 September 28th 04 03:43 PM
passing variables from an excel macro to a powerpoint macro jake Excel Programming 1 December 11th 03 02:36 AM


All times are GMT +1. The time now is 03:24 PM.

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

About Us

"It's about Microsoft Excel"