Thanks Thanks:  0
Likes Likes:  0
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Run Macro from VBA

  1. #1
    Board Regular
    Join Date
    Apr 2003
    Posts
    54
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Run Macro from VBA

    How do I run a MACRO from a VBA script (from a button)??

    Thanks,


    - Jeff

  2. #2
    MrExcel MVP
    Moderator
    Smitty's Avatar
    Join Date
    May 2003
    Location
    Redmond, WA
    Posts
    29,535
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Run Macro from VBA

    If you mean a button placed on an Excel worksheet, then there are two ways:

    1) If it's a button created from the Control Toolbox, then double-click the button in design mode and you'll get
    Code:
    Private Sub cb2_Click()
        '   Put your code here
    End Sub
    2) If it's a Fomsbutton, Right click the button and select "Assign Macro".

    If you want to test your macro from within VBA, hit F5.

    HTH,

    Smitty

  3. #3
    Board Regular
    Join Date
    Apr 2003
    Posts
    54
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run Macro from VBA

    Thanks Smitty for your post. I guess what I needed to know is what is the code that needs to go in the spot where you have ' Put your code here.


    Thanks again,

    - Jeff

  4. #4
    MrExcel MVP
    Moderator
    Smitty's Avatar
    Join Date
    May 2003
    Location
    Redmond, WA
    Posts
    29,535
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Run Macro from VBA

    I guess what I needed to know is what is the code that needs to go in the spot where you have ' Put your code here .
    That's kind of a relative question, because it's entirely dependent upon what you're trying to do. If you have written or recorded a macro and you want it to be triggered when a button is clicked, then copy that code and put in place of 'Put your code here.

    I.E. if you're trying to bring up a user form with the checkboxes that you mentioned in your previous post, you'd do this:
    Code:
    Private Sub CommandButton1_Click()
      UserForm1.Show
    End Sub
    Note that the code for Control Toolbox button's goes in the sheet's module, whereas a macro assigned to a Forms button needs to be in a general module.

    I can e-mail you a workbook with various button examples if you'd like.

    Hope that helps,

    Smitty

  5. #5
    New Member
    Join Date
    Oct 2004
    Posts
    1
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run Macro from VBA

    DoCmd.RunMacro ""

  6. #6
    Board Regular
    Join Date
    Dec 2002
    Posts
    208
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run Macro from VBA

    Try this,

    Application.Run "PERSONAL.XLS!Name of your Macro here"

    If your macro is not stored in your PERSONAL.XLS, place the path to the location of the file.

    Hope this helps



    Jack
    An expert is a man who has made all the mistakes which can be made, in a narrow field.
    Niels Bohr (1885-1962)

  7. #7
    New Member
    Join Date
    Aug 2010
    Posts
    1
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Re: Run Macro from VBA

    If you want to run a macro already saved inyour macros in excel then use this:
    Application.Run "'Nameofspreadsheet'!NameOfMacro"

    K.

  8. #8
    New Member
    Join Date
    Sep 2010
    Posts
    2
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run Macro from VBA

    Quote Originally Posted by N1njad0g View Post
    If you want to run a macro already saved inyour macros in excel then use this:
    Application.Run "'Nameofspreadsheet'!NameOfMacro"

    K.
    How do I make the part in quotes dynamic?

    For example, in my macro I've defined Sheetname as the name of the spreadsheet and Macroname as the name of the macro.

    Now I want to cycle through different worksheets and macros using the Application.Run code above.

    How would I do that?

    I tried
    Code:
    Application.Run Sheetname!Macroname
    but that gives me the following error: Run-time error '424': Object required

    I think it means I have to use quotation marks in there somewhere. Just can't figure out how. Can anybody help?

  9. #9
    Board Regular
    Join Date
    Oct 2003
    Posts
    1,325
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run Macro from VBA

    Have you tried:

    Application.Run Sheetname & "!" & Macroname

  10. #10
    New Member
    Join Date
    Sep 2010
    Posts
    2
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Run Macro from VBA

    Quote Originally Posted by doofusboy View Post
    Have you tried:

    Application.Run Sheetname & "!" & Macroname
    Unfortunately that doesn't work. I get a "Compile error: Expected: end of statement" when I try that.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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