PC Review


Reply
Thread Tools Rate Thread

call macro with variable

 
 
Whit
Guest
Posts: n/a
 
      2nd Apr 2008
How can I call a macro when the name is a variable


MacroName = "example"
While i <= 14
MacroName = MacroName & i
Call MacroName (path)
i = i + 1
Loop

Cheers



 
Reply With Quote
 
 
 
 
paul.robinson@it-tallaght.ie
Guest
Posts: n/a
 
      2nd Apr 2008
Hi
You don't use brackets.
Call MacroName

or if it has parameters

Call MacroName variable1, variable2, etc

regards
Paul

On Apr 2, 10:01*am, Whit <W...@discussions.microsoft.com> wrote:
> How can I call a macro when the name is a variable
>
> * * MacroName = "example"
> * * While i <= 14
> * * * * MacroName = MacroName & i
> * * * * Call MacroName (path)
> * * * * i = i + 1
> * * Loop
>
> Cheers


 
Reply With Quote
 
Whit
Guest
Posts: n/a
 
      2nd Apr 2008
I get a compile error...

"(E-Mail Removed)" wrote:

> Hi
> You don't use brackets.
> Call MacroName
>
> or if it has parameters
>
> Call MacroName variable1, variable2, etc
>
> regards
> Paul
>
> On Apr 2, 10:01 am, Whit <W...@discussions.microsoft.com> wrote:
> > How can I call a macro when the name is a variable
> >
> > MacroName = "example"
> > While i <= 14
> > MacroName = MacroName & i
> > Call MacroName (path)
> > i = i + 1
> > Loop
> >
> > Cheers

>
>

 
Reply With Quote
 
paul.robinson@it-tallaght.ie
Guest
Posts: n/a
 
      2nd Apr 2008
Hi
didn't test this before. Looks like you simply cannot call a variable
name macro.
I'd suggest you create one macro with a variable parameter

Sub myMacro (i as integer)
'code depending on i
end sub

Then in the loop do
Call myMacro i

regards
Paul
On Apr 2, 10:16*am, Whit <W...@discussions.microsoft.com> wrote:
> I get a compile error...
>
>
>
> "paul.robin...@it-tallaght.ie" wrote:
> > Hi
> > You don't use brackets.
> > * * * * Call MacroName

>
> > or if it has parameters

>
> > * * * * Call MacroName variable1, variable2, etc

>
> > regards
> > Paul

>
> > On Apr 2, 10:01 am, Whit <W...@discussions.microsoft.com> wrote:
> > > How can I call a macro when the name is a variable

>
> > > * * MacroName = "example"
> > > * * While i <= 14
> > > * * * * MacroName = MacroName & i
> > > * * * * Call MacroName (path)
> > > * * * * i = i + 1
> > > * * Loop

>
> > > Cheers- Hide quoted text -

>
> - Show quoted text -


 
Reply With Quote
 
Ivyleaf
Guest
Posts: n/a
 
      2nd Apr 2008
Hi,

To my understanding there is no way to call a sub from a name
variable. Rather than passing a parameter to a sub with different
'sections' though, wouldn't it be easier to move the 'if' back into
the first sub?

sub Macro1()
Dim NextSub
do some things
NextSub = whatever you choose

Select Case NextSub
Case "Macro2"
Call Macro2
Case "Macro3"
Call Macro3
etc. etc.
End Select
End Sub

Hard to say withouu seeing you code.

Cheers,
Ivan.

On Apr 2, 10:21*pm, paul.robin...@it-tallaght.ie wrote:
> Hi
> didn't test this before. Looks like you simply cannot call a variable
> name macro.
> I'd suggest you create one macro with a variable parameter
>
> Sub myMacro (i as integer)
> * * 'code depending on i
> end sub
>
> Then in the loop do
> Call myMacro i
>
> regards
> Paul
> On Apr 2, 10:16*am, Whit <W...@discussions.microsoft.com> wrote:
>
>
>
> > I get a compile error...

>
> > "paul.robin...@it-tallaght.ie" wrote:
> > > Hi
> > > You don't use brackets.
> > > * * * * Call MacroName

>
> > > or if it has parameters

>
> > > * * * * Call MacroName variable1, variable2, etc

>
> > > regards
> > > Paul

>
> > > On Apr 2, 10:01 am, Whit <W...@discussions.microsoft.com> wrote:
> > > > How can I call a macro when the name is a variable

>
> > > > * * MacroName = "example"
> > > > * * While i <= 14
> > > > * * * * MacroName = MacroName & i
> > > > * * * * Call MacroName (path)
> > > > * * * * i = i + 1
> > > > * * Loop

>
> > > > Cheers- Hide quoted text -

>
> > - Show quoted text -- Hide quoted text -

>
> - Show quoted text -


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      2nd Apr 2008
You could use application.run

Option Explicit
Sub testme1(mystr As String)
MsgBox mystr & " from 1"
End Sub
Sub testme2(mystr As String)
MsgBox mystr & " from 2"
End Sub
Sub testme3(mystr As String)
MsgBox mystr & " from 3"
End Sub
Sub testme4(mystr As String)
MsgBox mystr & " from 4"
End Sub
Sub aaa()
Dim iCtr As Long
For iCtr = 1 To 4
Application.Run "testme" & iCtr, "hi there"
Next iCtr
End Sub



Whit wrote:
>
> How can I call a macro when the name is a variable
>
> MacroName = "example"
> While i <= 14
> MacroName = MacroName & i
> Call MacroName (path)
> i = i + 1
> Loop
>
> Cheers


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a variable from a macro in a separate workbook to the call Spike Microsoft Excel Programming 3 5th Nov 2009 09:09 AM
Use a variable to call a variable preiously set. Richhall Windows XP General 8 19th Sep 2008 03:34 PM
Call VBScript from VBA, pass variable, return result to macro? Ed Microsoft Excel Programming 26 11th Jul 2006 07:07 PM
Running a variable macro when any value is entered into a variable cell IP Microsoft Excel Programming 3 14th Dec 2005 05:15 PM
Define worksheet as variable, then call in macro bugman Microsoft Excel Programming 2 22nd Nov 2005 05:13 PM


Features
 

Advertising
 

Newsgroups
 


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