+ Reply to Thread
Results 1 to 6 of 6

Run macro from name stored in variable?

  1. #1
    Registered User
    Join Date
    01-05-2006
    Posts
    6

    Question Run macro from name stored in variable?

    can you run a macro using its name stored in a variable?

    e.g.:

    ======================
    mName = "Macro1"

    Call mName
    ========================

    (I know the above doesn't work, but is there any similar method that does work?)

    Many thanks.

  2. #2
    Chip Pearson
    Guest

    Re: Run macro from name stored in variable?

    Use Application.Run.

    Application.Run mName


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    "chubster264"
    <[email protected]> wrote
    in message
    news:[email protected]...
    >
    > can you run a macro using its name stored in a variable?
    >
    > e.g.:
    >
    > ======================
    > *mName = "Macro1"
    >
    > Call mName *
    > ========================
    >
    > (I know the above doesn't work, but is there any similar method
    > that
    > does work?)
    >
    > Many thanks.
    >
    >
    > --
    > chubster264
    > ------------------------------------------------------------------------
    > chubster264's Profile:
    > http://www.excelforum.com/member.php...o&userid=30164
    > View this thread:
    > http://www.excelforum.com/showthread...hreadid=498463
    >




  3. #3
    Registered User
    Join Date
    01-05-2006
    Posts
    6

    Question

    Thanks for the suggestion - I'm getting an error 1004 though when I run the code as below... Any further suggestions as to what might be the problem?


    Sub temp1()

    MsgBox "hello"

    End Sub

    ---------------------

    Sub trythemacro()

    Dim mName As String

    mName = "temp1"

    Application.Run (mName)

    End Sub

  4. #4
    Chip Pearson
    Guest

    Re: Run macro from name stored in variable?

    Your code works fine for me. Try removing the parentheses from
    the Application.Run statement.


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    "chubster264"
    <[email protected]> wrote
    in message
    news:[email protected]...
    >
    > Thanks for the suggestion - I'm getting an error 1004 though
    > when I run
    > the code as below... Any further suggestions as to what might
    > be the
    > problem?
    >
    >
    >
    > Sub temp1()
    >
    > MsgBox "hello"
    >
    > End Sub
    >
    > ---------------------
    >
    > Sub trythemacro()
    >
    > Dim mName As String
    >
    > mName = "temp1"
    >
    > Application.Run (mName)
    >
    > End Sub
    >
    >
    > --
    > chubster264
    > ------------------------------------------------------------------------
    > chubster264's Profile:
    > http://www.excelforum.com/member.php...o&userid=30164
    > View this thread:
    > http://www.excelforum.com/showthread...hreadid=498463
    >




  5. #5
    Chip Pearson
    Guest

    Re: Run macro from name stored in variable?

    I should have added that your called code must be in a regular
    code module, not a sheet module, form module, or the ThisWorkbook
    code module.


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    "Chip Pearson" <[email protected]> wrote in message
    news:%[email protected]...
    > Your code works fine for me. Try removing the parentheses from
    > the Application.Run statement.
    >
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    >
    > "chubster264"
    > <[email protected]>
    > wrote in message
    > news:[email protected]...
    >>
    >> Thanks for the suggestion - I'm getting an error 1004 though
    >> when I run
    >> the code as below... Any further suggestions as to what might
    >> be the
    >> problem?
    >>
    >>
    >>
    >> Sub temp1()
    >>
    >> MsgBox "hello"
    >>
    >> End Sub
    >>
    >> ---------------------
    >>
    >> Sub trythemacro()
    >>
    >> Dim mName As String
    >>
    >> mName = "temp1"
    >>
    >> Application.Run (mName)
    >>
    >> End Sub
    >>
    >>
    >> --
    >> chubster264
    >> ------------------------------------------------------------------------
    >> chubster264's Profile:
    >> http://www.excelforum.com/member.php...o&userid=30164
    >> View this thread:
    >> http://www.excelforum.com/showthread...hreadid=498463
    >>

    >
    >




  6. #6
    Registered User
    Join Date
    01-05-2006
    Posts
    6
    Chip,

    Thanks so much! That nailed it.



    Quote Originally Posted by Chip Pearson
    I should have added that your called code must be in a regular
    code module, not a sheet module, form module, or the ThisWorkbook
    code module.


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1