Run Macro from VBA

jbowes99

Board Regular
Joined
Apr 9, 2003
Messages
54
How do I run a MACRO from a VBA script (from a button)??

Thanks,


- Jeff
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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
 

jbowes99

Board Regular
Joined
Apr 9, 2003
Messages
54
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
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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
 

weareu

New Member
Joined
Oct 13, 2004
Messages
1

ADVERTISEMENT

DoCmd.RunMacro "<macro name>"
 

Jack_58

Board Regular
Joined
Dec 3, 2002
Messages
208
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
 

N1njad0g

New Member
Joined
Aug 27, 2010
Messages
1

ADVERTISEMENT

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

K.
 

d0gers

New Member
Joined
Sep 15, 2010
Messages
2
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?
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,151,766
Messages
5,766,349
Members
425,350
Latest member
procha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top