Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,090
Default Calling a macro

Excel XP, Win XP
I have a number of macros, say Able, Baker, Charlie, etc.
I have a variable named MacroToRun as string.
I have code that sets the value of MacroToRun as, say, "Charley".
I want to run the macro whose name is in the variable MacroToRun.
How do I call that macro?
Thanks for your time. Otto


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Calling a macro

Sub Charley()
MsgBox "Charley"
End Sub
Sub Tester1()
MacrotoRun = "Charley"
Application.Run MacrotoRun

End Sub

--
Regards,
Tom Ogilvy






"Otto Moehrbach" wrote in message
...
Excel XP, Win XP
I have a number of macros, say Able, Baker, Charlie, etc.
I have a variable named MacroToRun as string.
I have code that sets the value of MacroToRun as, say, "Charley".
I want to run the macro whose name is in the variable MacroToRun.
How do I call that macro?
Thanks for your time. Otto



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Calling a macro

To add to what Tom said (since it is an issue that caused me some grief
the first time I used Application.run) if your macros reside in a Sheet
module as opposed to a general code module then it seems to be the case
that you need to qualify the macro names - even if you are calling them
from the same sheet. For example, in Sheet1 the following works fine:

Sub Greet(who As String)
MsgBox "Hello " & who
End Sub

Sub CallIt()
Application.Run "Sheet1.Greet", "World"
End Sub

But replacing "Sheet1.Greet" by "Greet" yields "Run time error '1004':
application or object defined error" (or words to that effect).

HTH

-John Coleman

Otto Moehrbach wrote:
Excel XP, Win XP
I have a number of macros, say Able, Baker, Charlie, etc.
I have a variable named MacroToRun as string.
I have code that sets the value of MacroToRun as, say, "Charley".
I want to run the macro whose name is in the variable MacroToRun.
How do I call that macro?
Thanks for your time. Otto


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,090
Default Calling a macro

Thanks Tom. I knew it had to be something simple. Otto
"Tom Ogilvy" wrote in message
...
Sub Charley()
MsgBox "Charley"
End Sub
Sub Tester1()
MacrotoRun = "Charley"
Application.Run MacrotoRun

End Sub

--
Regards,
Tom Ogilvy






"Otto Moehrbach" wrote in message
...
Excel XP, Win XP
I have a number of macros, say Able, Baker, Charlie, etc.
I have a variable named MacroToRun as string.
I have code that sets the value of MacroToRun as, say, "Charley".
I want to run the macro whose name is in the variable MacroToRun.
How do I call that macro?
Thanks for your time. Otto





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,090
Default Calling a macro

John
Correct me if I'm wrong with this, but all macros in a sheet macro are
event macros. That raises the question in my mind "Why would I want to run
an event macro at any time other than at the occurrence of the event?
Apparently, you wanted to do so at one time so maybe all sheet macros are
not event macros? Otto
"John Coleman" wrote in message
oups.com...
To add to what Tom said (since it is an issue that caused me some grief
the first time I used Application.run) if your macros reside in a Sheet
module as opposed to a general code module then it seems to be the case
that you need to qualify the macro names - even if you are calling them
from the same sheet. For example, in Sheet1 the following works fine:

Sub Greet(who As String)
MsgBox "Hello " & who
End Sub

Sub CallIt()
Application.Run "Sheet1.Greet", "World"
End Sub

But replacing "Sheet1.Greet" by "Greet" yields "Run time error '1004':
application or object defined error" (or words to that effect).

HTH

-John Coleman

Otto Moehrbach wrote:
Excel XP, Win XP
I have a number of macros, say Able, Baker, Charlie, etc.
I have a variable named MacroToRun as string.
I have code that sets the value of MacroToRun as, say, "Charley".
I want to run the macro whose name is in the variable MacroToRun.
How do I call that macro?
Thanks for your time. Otto






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Calling a macro

Otto,
General subs and functions can be in a sheet macro module. Often
event handlers are easier to understand if you modularize and place
some of the logic in separate subs or functions - and if they are only
to be used in Sheet1 it makes some sense to leave these subs and
functions in the Sheet1 module (though there would be nothing wrong
with placing them in a general code module). Also - I tend to use event
handlers, especially button click events, as little more than
dispathers which call an appropriate sub. In this context
Application.Run could have a useful role inside of a sheet module. You
are thus technically wrong about sheet macros but you are right in the
sense that sheet macros are intended primarily for even handlers.

-John Coleman

Otto Moehrbach wrote:
John
Correct me if I'm wrong with this, but all macros in a sheet macro are
event macros. That raises the question in my mind "Why would I want to run
an event macro at any time other than at the occurrence of the event?
Apparently, you wanted to do so at one time so maybe all sheet macros are
not event macros? Otto
"John Coleman" wrote in message
oups.com...
To add to what Tom said (since it is an issue that caused me some grief
the first time I used Application.run) if your macros reside in a Sheet
module as opposed to a general code module then it seems to be the case
that you need to qualify the macro names - even if you are calling them
from the same sheet. For example, in Sheet1 the following works fine:

Sub Greet(who As String)
MsgBox "Hello " & who
End Sub

Sub CallIt()
Application.Run "Sheet1.Greet", "World"
End Sub

But replacing "Sheet1.Greet" by "Greet" yields "Run time error '1004':
application or object defined error" (or words to that effect).

HTH

-John Coleman

Otto Moehrbach wrote:
Excel XP, Win XP
I have a number of macros, say Able, Baker, Charlie, etc.
I have a variable named MacroToRun as string.
I have code that sets the value of MacroToRun as, say, "Charley".
I want to run the macro whose name is in the variable MacroToRun.
How do I call that macro?
Thanks for your time. Otto



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,090
Default Calling a macro

Thanks John. I've learned something again. There is no end to this
learning thing. Otto
"John Coleman" wrote in message
ups.com...
Otto,
General subs and functions can be in a sheet macro module. Often
event handlers are easier to understand if you modularize and place
some of the logic in separate subs or functions - and if they are only
to be used in Sheet1 it makes some sense to leave these subs and
functions in the Sheet1 module (though there would be nothing wrong
with placing them in a general code module). Also - I tend to use event
handlers, especially button click events, as little more than
dispathers which call an appropriate sub. In this context
Application.Run could have a useful role inside of a sheet module. You
are thus technically wrong about sheet macros but you are right in the
sense that sheet macros are intended primarily for even handlers.

-John Coleman

Otto Moehrbach wrote:
John
Correct me if I'm wrong with this, but all macros in a sheet macro
are
event macros. That raises the question in my mind "Why would I want to
run
an event macro at any time other than at the occurrence of the event?
Apparently, you wanted to do so at one time so maybe all sheet macros are
not event macros? Otto
"John Coleman" wrote in message
oups.com...
To add to what Tom said (since it is an issue that caused me some grief
the first time I used Application.run) if your macros reside in a Sheet
module as opposed to a general code module then it seems to be the case
that you need to qualify the macro names - even if you are calling them
from the same sheet. For example, in Sheet1 the following works fine:

Sub Greet(who As String)
MsgBox "Hello " & who
End Sub

Sub CallIt()
Application.Run "Sheet1.Greet", "World"
End Sub

But replacing "Sheet1.Greet" by "Greet" yields "Run time error '1004':
application or object defined error" (or words to that effect).

HTH

-John Coleman

Otto Moehrbach wrote:
Excel XP, Win XP
I have a number of macros, say Able, Baker, Charlie, etc.
I have a variable named MacroToRun as string.
I have code that sets the value of MacroToRun as, say, "Charley".
I want to run the macro whose name is in the variable MacroToRun.
How do I call that macro?
Thanks for your time. Otto




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
calling next macro Michelle Hanan Excel Programming 1 July 18th 06 06:17 PM
calling a macro jhahes[_25_] Excel Programming 6 August 4th 05 12:22 AM
Calling a variable macro Hari Prasadh[_2_] Excel Programming 2 July 30th 05 09:28 AM
Calling a macro from a key RWN Excel Programming 0 February 8th 05 05:19 AM
Calling macro in add-in. Clark B Excel Programming 1 July 24th 03 11:05 PM


All times are GMT +1. The time now is 01:38 AM.

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"