Consulting

Results 1 to 7 of 7

Thread: Use a variable for subroutine name

  1. #1

    Use a variable for subroutine name

    I have many macros in a workbook but only 7 or 8 are normally used. Sometimes the particular macro name needed is lost in this list of macros. I added a worksheet (named "DOC") which lists the most common macros with a brief description of their function. Now I'd like to highlight the macro name I want and call it. Although the following code doesn't do this, it has the same problem. How do I fix it?

    The code asks the user for a macro name and then attempts to call that macro. It gets a compile error because the variable name for the macro isn't allowed. If I can get this to work, I can solve the real problem.

    Sub VarMac()

    Dim macName as String

    macName = InputBox("Please enter the macro name.")

    MsgBox macName

    Call macName

    End

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The key word "Private" keeps Procedures from being listed in the Macro list
    Private Sub Blahblah()
    Private Function Blehbleh()
    The Module directive "Option Private Module" keeps all Procedures in that Module from being listed
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Sub VarMac()
    Dim macName As String
    macName = InputBox("Please enter the macro name.")
    MsgBox macName
    Application.Run macName
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by jwise View Post
    I have many macros in a workbook but only 7 or 8 are normally used. Sometimes the particular macro name needed is lost in this list of macros. I added a worksheet (named "DOC") which lists the most common macros with a brief description of their function. Now I'd like to highlight the macro name I want and call it. Although the following code doesn't do this, it has the same problem. How do I fix it?

    The code asks the user for a macro name and then attempts to call that macro. It gets a compile error because the variable name for the macro isn't allowed. If I can get this to work, I can solve the real problem.
    I'd add a 'Control Panel' worksheet with 7-8 buttons each calling the appropriate macro (or an UserForm if I wanted to get fancy)

    It doesn't seen very user friendly to make people type in the name of a macro to run.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5

    Thanks

    Thank you for all the replies. VBAX is a wonderful site and resource.

    "Application.Run" was exactly what I needed. I've tested the sample code and it performed as desired.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    You overlooked the very sensible comments Paul_Hossler made.

  7. #7
    I appreciate every reply since the responder is taking his/her time to help me. I did not overlook any reply; I made a value judgement that the improved function was not worth the time to code and test. I decided it wasn't since the code was really for my use only. Had this been code that I would have sold or provided to others, the decision would have probably been different. Thanks for your input.

Posting Permissions

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