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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
If you want to run a macro already saved inyour macros in excel then use this:
Application.Run "'Nameofspreadsheet'!NameOfMacro"

K.
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,344
Members
448,570
Latest member
rik81h

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
Back
Top