PTS Blog

Main menu:

Excel Books

Books that I own and use while developing in Excel

Goods and Services

Excel or charting related products and services which I use or feel are worthwhile additions

Excel User Conference

2008 US East Coast Excel User Conference
September 24-26, 2008
Atlantic City, NJ

Subscribe

Site search


Recent Posts

Recently Commented

March 2008
S M T W T F S
« Feb   Apr »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Archive


 

Categories


 

How To: Assign a Macro to a Button or Shape

by Jon Peltier
Peltier Technical Services, Inc., Copyright © 2008. All rights reserved.

In recent posts, I've discussed recording macros, fixing recorded macros, and even using someone else's macros. To run a macro, you have a few choices.

  • Tools menu > Macro > Macros, select the Macro, and click Run
  • Shortcut Key combo Alt+F8, select the Macro, and click Run
  • Run Macro button on the Visual Basic toolbar, select the Macro, and click Run
  • Click in the macro with the mouse and press F5

None of these techniques are particularly elegant, and you wouldn't want to email a file with a few macros to a colleague and tell them, "Okay, go to the Tools menu, select Macro, then select Macro, then click on MyFirstMacro in the list, and click Run." It would be nice to just tell them, "Click the First Macro button."

You can assign a macro to a few types of objects:

In this post we'll look at using a Forms menu button to run our macros. We'll use this simple macro for our example.

Visual Basic:
  1. Sub HelloWorld()
  2.     MsgBox "Hello, World!   ", vbExclamation
  3. End Sub

When it is run, we see a simple message.

Hello World

Forms Menu Controls

First make the Forms toolbar visible. Like so many other things in Excel, there are several ways to accomplish this.

  • Tools menu > Customize > Toolbars tab, check the box in front of Forms
  • View menu > Toolbars, select Forms from the list
  • Right click on the menu and toolbar area, choose Forms from the list

The Forms toolbar will appear.

Forms Menu

Select the Button button, then draw a rectangle in the worksheet where you want to place the button (you can move and resize it later). The Assign Macro dialog pops up with a list of macros in the active worksheet for you to select from.

Assign Macro

Select a macro and click OK, or Cancel (you can select a macro at a future time). You can edit the text of the button after you dismiss the dialog, or right click on the button any time.

You can assign a macro to any of the Forms toolbar controls.

Shapes

You can assign a macro to any shape (including any chart) on a worksheet. Right click on the shape, choose Assign Macro, and follow the steps above.

Assign Macro

Share/Save/Bookmark

Comments

Comment from Tony
Time: Thursday, March 13, 2008, 7:39 am

Great post! I am a big fan of assigning macros to a button so the user can easily run ‘em without having to go through the menu.

One thing that typically trips people up is the default security setting for Excel. Many times, if you send someone an .xls file with a macro, they either need to approve the sender or set their security lower. Otherwise, they won’t be able to run the macro. I’m sure most readers know this, but I think it’s worth bringing up.

Comment from Jon Peltier
Time: Thursday, March 13, 2008, 8:59 am

Tony - Good point. One of these days I’ll blog about macro security.

Comment from Andras
Time: Tuesday, March 18, 2008, 9:41 am

John,

I’m very helpful for your site - it gave me many help in my job.
In addition your blog is also very useful - but as you’ve wrote, it took a bit time to find it.

However, please let me ask a question regarding to this blog entry.
Is there any way to assign an UDF to a button or to a menu/toolbar item in Excel?.

When I’ve tried to assign to a toolbar some of my UDFs stored in my personal.xls, but only the simple VBA routines appear, not the UDFs.

I’m currently using MS Excel 2002.

I’d be very much appreciated if you could share us your experiences in this.

Thank you for your help in advance

Kind regards,
Andras Ujszaszy

Comment from Jon Peltier
Time: Tuesday, March 18, 2008, 1:41 pm

Hi Andras, By definition a UDF is used in a cell or in another VBA procedure and simply returns a value. When you click on a button you are not so much expecting to get a value as having some action perfomred. How would your UDF work from a button click?

Write a comment





Create Excel dashboards quickly with Plug-N-Play reports.