- Social Media
- Smart Devices
How to Create a UserForm List Box in Outlook
By Allen Bethea
Microsoft Outlook's Visual Basic for Applications macro programming language enables you to add a form to an email message that can collect data from its recipient. For example, you can insert a VBA UserForm and list box into your message that displays multiple items or options the recipient can choose from. Before you can add this feature to your messages, however, you need to add the Developer tab and its tools to the ribbon and change Outlook's security settings to allow VBA macros to run.
Click the "File" tab, "Options" and then "Customize Ribbon."
Check the box labeled "Developer" under Main Tabs and then click "OK." Outlook will automatically return to the Home panel.
Click the “File" tab and then "Options," "Trust Center" and "Trust Center Settings.”
Select "Macro Settings," "Enable all macros" and then click "OK" to allow the list box macro run during testing. Once you are done with your UserForm, however, you can change your settings to "Notifications for all macros" for more security.
Restart Outlook to activate the macro security settings change.
Click the "Developer" tab, "Macros" and then the "Macros" option.
Type a name for your macro and then click "Create."
Click the "Insert UserForm" button to display the form toolbox and create the UserForm that will contain your listbox.
Click the "Listbox” control in the Toolbox, hold down the mouse button, drag the control to the UserForm and then release it.
Click the "CommandButton” control in the Toolbox, hold down the mouse button, drag it to the UserForm and then release it.
Right-click the “CommandButton” in the UserForm and then click "View Code" to display its VBA macro code.
Add items to the list box using the AddItem function. For example, if you want the list box to contain the three primary colors, your code should resemble the following:
Private Sub CommandButton1_Click() ListBox1.AddItem "Red" ListBox1.AddItem "Green" ListBox1.AddItem "Blue" End Sub
ListBox1 is the name Outlook automatically gives the list box you dragged to the UserForm. The statement ListBox1.AddItem "Red" makes Red the first item in the list box.
Click "Save," press "F5" and then click the CommandButton to run the macro. When you run the macro and click the "CommandButton" the colors Red, Green and Blue will appear in the list box.
- Microsoft: ListBox.AddItem Method (Outlook Forms Script)
- Microsoft: ListBox Object (Outlook Forms Script)
- Microsoft: View My Options and Settings in the Trust Center
- Microsoft: CommandButton Object (Outlook Forms Script)
- Information in this article applies to Outlook 2013. It may vary slightly or significantly with other versions of Outlook.
Allen Bethea has written articles on programming, web design,operating systems and computer hardware since 2002. He holds a Bachelor of Science from UNC-Chapel Hill and AAS degrees in office technology, mechanical engineering/drafting and internet technology. Allen has extensive experience with desktop and system software for both Windows and Linux operating systems.
Assign a macro to a Form or a Control button
You can use a Form control button or a command button (an ActiveX control) to run a macro that performs an action when a user clicks it. Both these buttons are also known as a push button, which can be set up to automate the printing of a worksheet, filtering data, or calculating numbers. In general, a Form control button and an ActiveX control command button are similar in appearance and function. However, they do have a few differences, which are explained in the following sections.
In the sections below, learn how to add a macro to a button in Excel—for Windows or the Mac.
Note: ActiveX controls are not supported on the Mac.
Macros and VBA tools can be found on the Developer tab, which is hidden by default.
The first step is to enable it. For more information, see the article: Show the Developer tab .
Add a button (Form control)
Click the worksheet location where you want the upper-left corner of the button to appear. The Assign Macro popup window appears.
Assign a macro to the button, and then click OK .
To specify the control properties of the button, right-click the button, and then click Format Control .
Add a command button (ActiveX control)
Click the worksheet location at which you want the upper-left corner of the command button to appear.
In the Controls group, click View Code . This launches the Visual Basic Editor. Ensure that Click is chose in the drop-down list on the right. The sub procedure CommandButton1_Click (see the figure below) runs these two macros when the button is clicked: SelectC15 and HelloMessage .
In the subprocedure for the command button, do either of the following:
Enter the name of an existing macro in the workbook. You can find macros by clicking Macros in the Code group. You can run multiple macros from a button by entering the macro names on separate lines inside the subprocedure.
As necessary, add your own VBA code.
To run the VBA code that is now part of the button, click the ActiveX command button that you just created.
To edit the ActiveX control, make sure that you are in design mode. On the Developer tab, in the Controls group, turn on Design Mode .
Note: Before you click Properties , make sure that the object for which you want to examine or change properties is already selected.
The Properties box appears. For detailed information about each property, select the property, and then press F1 to display a Visual Basic Help topic. You can also type the property name in the Visual Basic Help Search box. The following table summarizes the properties that are available.
Macros and VBA tools can be found on the Developer tab, which is hidden by default, so the first step is to enable it
Go to Excel > Preferences… > Ribbon & Toolbar .
Follow these steps:
Note: If you have already inserted a button, you can right-click on it, and select Assign Macro .
Assign a macro to the button and click OK .
To specify the control properties of the button, right-click it, and then select Format Control... .
Add a command button (Visual Basic control)
Click the worksheet location where you want the upper-left corner of the command button to appear.
In the Assign Macro dialog box, select New , which will open the Visual Basic Editor ( VBE ) to a pane with the following code:
In the subprocedure for the command button, between the Sub and End Sub lines, do either of the following:
Enter the name of an existing macro in the workbook. You can run multiple macros from a button by typing the macro names on separate lines inside the sub procedure
Add your own VBA code.
To edit the button, right-click it and choose Visual Basic .
To specify the control properties of the button, right-click it, and then select Format Control... .
Need more help?
You can always ask an expert in the Excel Tech Community or get support in Communities .
Add or edit a macro for a control on a worksheet
Enable or disable macros in Office files
Enable or disable ActiveX settings in Office files
Want more options?
Explore subscription benefits, browse training courses, learn how to secure your device, and more.
Microsoft 365 subscription benefits
Microsoft 365 training
Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.
Ask the Microsoft Community
Microsoft Tech Community
Microsoft 365 Insiders
Was this information helpful?
Thank you for your feedback.
- Search forums
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature currently requires accessing the site using the built-in Safari browser.
- If you would like to post, please check out the MrExcel Message Board FAQ and register here . If you forgot your password, you can reset your password .
- Question Forums
- Excel Questions
vba code to assign macros
- Thread starter John
- Start date Mar 25, 2002
- Mar 25, 2002
I am having trouble re-assigning macros to buttons(shapes?) using vba. I have tried using the following: ActiveSheet.Shapes("Button 3").Select Selection.OnAction = "Data_1" ActiveSheet.Shapes("Button 1").Select Selection.OnAction = "Data_2" I created these buttons from the forms toolbar in excel. Some sheets have 6 or more buttons. This works fine on the original worksheet but when I copy that sheet to another book the names "Button 3” change e.g button 3 might become button 5. Is there any method I can use in vba to do this. Thanks
Dave hawley, rest in peace.
Hi John Not to sure I follow but try this With Sheet1 .Shapes("Button 1").OnAction = "MyMacro1" .Shapes("Button 2").OnAction = "MyMacro2" End With
- Apr 1, 2002
Thanks Dave The problem was not in getting the macro to run but rather in that the button names changed. e.g on one sheet when I right click on the button the name in the top left command bar shows as button 22. When I copy this sheet the name changes to button 4. After playing with this sheet I discovered that every time I copied it it kept the button 4 name, so I have changed my macro accordingly. Interestingly not all buttons on a page change name. Regards John
Ivan F Moala
On 2002-04-01 21:00, John wrote: Thanks Dave The problem was not in getting the macro to run but rather in that the button names changed. e.g on one sheet when I right click on the button the name in the top left command bar shows as button 22. When I copy this sheet the name changes to button 4. After playing with this sheet I discovered that every time I copied it it kept the button 4 name, so I have changed my macro accordingly. Interestingly not all buttons on a page change name. Regards John Click to expand...
- Apr 2, 2002
Thanks Ivan I had tried naming buttons this way but they had always reverted to the original name. I have tried again using your suggestion and it works fine. On reflection it may have been I was trying to give it a new button number rather than a name and it is possible a button with the same number existed on the worksheet. Thanks for the useful and helpful advise Regards John
- Aug 11, 2023
- Aug 25, 2023
- Oct 18, 2023
- Oct 5, 2023
- Oct 7, 2023
Share this page.
We've detected that you are using an adblocker.
Which adblocker are you using.
Disable AdBlock Plus
Disable uBlock Origin
Assign a macro to a button
This tutorial shows how to assign a macro to a form control and activex control button using excel or vba.
EXCEL VBA EXPLANATION
METHOD 1. Assign a macro to a Form Control button
Method 1. assign a macro to an activex control button, explanation about how to assign a macro to a button.
How to Assign a Macro to a Button in Excel (Easy Guide)
- -- By Sumit Bansal
While there are many different ways to run a macro in Excel, none of those methods can be as easy and user-friendly as clicking on a button.
And for that to work, you need to assign a macro to a button first.
In this tutorial, I will show you a couple of ways to insert a button in Excel and then assign a macro to that button (or shape). Once done, as soon as a user clicks on the button, the macro VBA code would be executed.
For the purpose of this tutorial, I will be using the below VBA macro code (which simply selects cell A1 in the active sheet and enters the text “Good Morning” in it and colors it red).
The above VBA code is placed in a regular module in the VB Editor
Now let’s dive right in and see how you can assign this macro to a button or shape in Excel!
This Tutorial Covers:
Insert a Shape and Assign Macro to that Shape
While there are dedicated buttons that you can insert in the worksheet and then assign the macro to it, I will first cover how to assign a macro to a shape .
I personally love this method and prefer it over the rest two methods covered later. You can easily insert a shape (square or rectangle) and can make it look like a button.
And since it’s a shape, you can easily format it to look perfect with your existing formatting or brand colors.
Below are the steps to insert a shape in Excel:
- Resize the rectangle and format it (give it a border, color, shade if you want).
After you have done the above steps, you will have a rectangle shape in the worksheet, and now we will assign a macro to this shape.
Now let’s see how to assign a macro to this shape.
- Right-click on the shape on which you want to assign the macro
- In the Assign Macro dialog box, you will see a list of all the macros that you have in the workbook
- Click on OK
The selected macro has now been assigned to the shape.
Now when you hover the cursor over the shape, it will show the hand icon. which indicates that now this shape has become clickable.
And now if you click on the shape, it will run the assigned macro .
You can type any text within the shape to make it more intuitive (such as ‘Click here to run the macro’). To do this. right-click on the shape and then click on Edit Text. Now you can type within the text box shape.
Note that you won’t be able to click and run the macro when the shape has been selected (i.e., you see a border around the shape that appears when you select it), To make it clickable, hit the Escape key or click anywhere in the worksheet.
Also, when you have assigned the macro to the shape already, you will not be able to select it by using the left mouse key (as it has become clickable and left-click would now execute the macro). In that case, select the shape, hold the control key and then press the left key.
Keeping Shape Visible When you Hide/Resize Rows/Columns
In Excel. when you insert a shape, it sits over the cells – like a chart/object.
This also has a drawback that when you resize or hide rows/columns that have the shape over it, the shape also follows suit.
In the below example, the shape gets hidden when I hide the column on which it’s placed.
If you don’t want this to happen, follow the below steps:
- Right-click on the shape
- In the Format Shape pane (or dialog box in case you’re using Excel 2010 or prior versions), select Size and Properties
- Close the pane (or dialog box)
Now, when you resize rows/columns or hide these, the shape would stay in its place.
Assign a Macro to Form Control Button
If you’re not too concerned with the formatting of the button and are ok with regular gray buttons, you can quickly insert it from form control (or ActiveX control as shown next) and then assign a macro to it.
For this to work, you will need to have the Developer tab in your ribbon. If you don’t have it, here is a detailed step-by-step tutorial on getting the developer tab in the Excel ribbon .
Once you have the developer tab visible, you can use the below steps to quickly insert a button and assign a macro to it:
- Click anywhere on the worksheet. This will insert the button wherever you click and automatically open the ‘Assign Macro’ dialog box.
The above steps would insert a button that has the specified macro assigned to it.
By default, it would be a small button with text such as ‘Button’ written on it. You can change the text to whatever you want and can also change the shape of the button (by dragging the edges).
Since this is an object that is placed over the worksheet (just like shapes/charts), you can drag and place it anywhere in the worksheet.
One drawback of using the Form Control button is that you don’t have much control over the formatting. For example, you can not change the color from gray to something else.
Although there is a little bit of formatting that you can do with a Form control button, it’s nowhere close to what you can do with shapes.
You get these button formatting options when you right-click on the button and then click on Format Control.
This will open the Format Control dialog box where you can change the font type/color, size, alignment, etc.
One good thing about this button is that it doesn’t hide or resize when you hide the rows/columns or resize them. It would, however, move in case you change the height or width or the row/column over which the button is placed.
In case you don’t want the button to stay in its place, you can change the setting by following the below steps:
- Right-click on the button
- Click on Format Control
- Click on the Properties tab
Assign a Macro to an ActiveX Control Button
Apart from the Form Control button, there is also an ActiveX control button to which you can assign a macro.
In most cases, you won’t need to use the ActiveX control button, and I recommend you use it only when you completely understand what it is and you know what you’re doing.
This also, sometimes, make ActiveX a bit glitchy and unpredictable. So, while I cover it in this tutorial, I don’t recommend using ActiveX button and assign a macro to it.
To insert an ActiveX button and then assign a macro to it, follow the below steps:
- Click on the Developer tab
- In the Control group, click on Insert.
- Click anywhere on the worksheet. This will insert the button wherever you click.
- Double-click on the button and it will open the VB Editor backend where you can place the code for the ActiveX button
With ActiveX control, you get a lot more flexibility with a single button. For example, you can specify one macro to be run when you simply click on the button once and another macro when you double-click or even another one when you use the up/down arrow key.
Again, not something you need to be using in your regular work.
Hope you found this tutorial useful. If you’re interested in learning VBA, you can check out more in-depth Excel VBA tutorials here .
You may also like the following Excel tutorials:
- How to Record a Macro in Excel
- Creating a User Defined Function (UDF) in Excel VBA
- Excel VBA MsgBox [Message Box]
- Useful Excel Macro Examples for VBA Beginners
- How to Remove Macros From an Excel Workbook
- How to Enable Macros in Excel?
FREE EXCEL BOOK
Get 51 Excel Tips Ebook to skyrocket your productivity and get work done faster
1 thought on “how to assign a macro to a button in excel (easy guide)”.
Following the procedure here, I cannot assign a Macros from an *.xlam workbook (addin). Is there a way to do this? thx
Leave a Comment Cancel reply
BEST EXCEL TUTORIALS
Best Excel Shortcuts
Creating a Pivot Table
INDEX- MATCH Combo
Creating a Drop Down List
Recording a Macro
© TrumpExcel.com – Free Online Excel Training
- Ablebits blog
- Excel macro
How to run macro in Excel and create your own macro button
In this tutorial, we'll cover many different ways to run a macro in Excel - from the ribbon and VB Editor, with a custom keyboard shortcut, and by creating your own macro button.
Though running an Excel macro is a simple thing for experienced users, it might not be immediately obvious to beginners. In this article, you will learn several methods to run macros, some of which may completely change your way of interacting with Excel workbooks.
How to run a macro from Excel ribbon
One of the fastest ways to execute VBA in Excel is to run a macro from the Developer tab. If you have never dealt with VBA code before, you may need to activate the Developer tab first. And then, do the following:
Tip. If the Developer tab is not added to your Excel ribbon, press Alt + F8 to open the Macro dialog.
Run a macro with custom keyboard shortcut
If you execute a certain macro on a regular basis, you can assign a shortcut key to it. A shortcut can be added while recording a new macro and to an existing one. For this, carry out these steps:
- On the Developer tab, in the Code group, click Macros .
- For lowercase letters, the shortcut is Ctrl + letter .
- Close the Macro dialog box.
Tip. It is recommended to always use uppercase key combinations for macros ( Ctrl + Shift + letter ) not to override the default Excel shortcuts. For example, if you assign Ctrl + f to a macro, you will lose the ability to call the Find and Replace dialog.
How to run macro from VBA Editor
If you aim to become an Excel pro, then you should definitely know how to start a macro not only from Excel, but also from the Visual Basic Editor. The good news is that it's a lot easier than you might expect :)
- Press Alt + F11 to launch the Visual Basic Editor.
- In the Project Explorer window on the left, double-click the module containing your macro to open it.
- On the menu bar, click Run > Run Sub/UserForm .
- On the toolbar, click the Run Macro button (green triangle).
Alternatively, you can use one of the following shortcuts:
- Press F5 to run the entire code.
- Press F8 to run each code line separately. This is very useful when testing and debugging macros.
Tip. If you like operating Excel from you keyboard, this tutorial may come in handy: 30 most useful Excel keyboard shortcuts .
How to create a macro button in Excel
The traditional ways of running macros are not hard, but still might present a problem if you are sharing a workbook with someone who has no experience with VBA - they simply won't know where to look! To make running a macro really easy and intuitive for anyone, create your own macro button.
- Click anywhere in the worksheet. This will open the Assign Macro dialogue box.
- If the text does not fit in the button, make the button control bigger or smaller by dragging the sizing handles. When finished, click anywhere on the sheet to exit the edit mode.
Tip. You can also assign a macro to an existing button or other Form controls such as spin buttons or scrollbars. For this, right-click the control inserted in your worksheet and choose Assign Macro from the pop-up menu.
Create a macro button from a graphic object
Regrettably, it is not possible to customize the appearance of button controls, because of which the button we created a moment ago does not look very nice. To make a really beautiful Excel macro button, you can use shapes, icons, images, WordArt and other objects.
As an example, I'll show you how you can run a macro by clicking a shape:
- In your worksheet, click where you want to insert the shape object.
- Format your shape-button the way you want. For example, you can change the fill and outline colors or use one of the predefined styles on the Shape Format tab. To add some text to the shape, simply double-click it and start typing.
How to add a macro button to Quick Access Toolbar
The macro button inserted in a worksheet looks good, but adding a button to each and every sheet is time-consuming. To make your favorite macro accessible from anywhere, add it to the Quick Access Toolbar . Here's how:
- Right-click the Quick Access Toolbar and choose More Commands… from the context menu.
- In the Choose commands from list, select Macros .
- Click OK twice to close both dialog windows.
How to put a macro button on Excel ribbon
In case you have a few frequently used macros in your Excel toolbox, you may find it convenient to have a custom ribbon group, say My Macros , and add all popular macros to that group as buttons.
First, add a custom group to an existing tab or your own tab. For the detailed instructions, please see:
- How to create a custom ribbon tab
- How to add a custom group
And then, add a macro button to your custom group by performing these steps:
- Right-click the ribbon, and then click Customize the Ribbon .
- In the list tabs on the right, select your custom group.
- In the Choose commands from list on the left, select Macros .
- In the list of macros, choose the one you wish to add to the group.
- Click the Add button.
- Click OK to save your changes and close the main dialog box.
How to run a macro on opening a workbook
Sometimes you may want to run a macro automatically on opening a workbook, for example, to display some message, run script or clear a certain range. This can be done in two ways.
Run macro automatically by using Workbook_Open event
Below are the steps to create a macro that automatically runs whenever you open a specific workbook:
- Open the workbook in which you want the macro to be executed.
- Press Alt + F11 to open the Visual Basic Editor.
- In the Project Explorer, double click ThisWorkbook to open its Code window.
- In the Object list above the Code window, select Workbook . This creates an empty procedure for the Open event to which you can add your own code like shown in the screenshot below.
For example, the following code will display a welcome message each time the workbook is opened:
Trigger macro on workbook opening with Auto_Open event
Another way to run a macro automatically on workbook opening is by using the Auto_Open event. Unlike the Workbook_Open event, Auto_Open() should sit in a standard code module, not in ThisWorkbook .
Here are the steps to create such a macro:
- In the Project Explorer , right-click Modules , and then click Insert > Module .
- In the Code window, write the following code:
Here's an example of the real-life code that displays a message box on workbook opening:
Note! The Auto_Open event is deprecated and available for backwards compatibility. In most cases, it can be replaced with the Workbook_Open event. For more information, please see Workbook_Open vs. Auto_Open .
Now that you know lots of ways to run a macro in Excel, you just need to choose the one best suited for your needs. I thank you for reading and hope to see you on our blog next week!
You may also be interested in
- Excel macro tutorial for beginners
- How to record a macro in Excel
- How to insert VBA code
- How to enable macros in Excel
- Personal Macro Workbook in Excel
- User-defined functions vs VBA macros: pros and cons
Table of contents
I wrote a macro to hide certain rows and columns, print a specific selection and then unhide the pertinent columns and save the workbook. All this is attached to a button. When it prints I get blank pages
Range("b1:F117").Select Selection.PrintOut Copies:=1, Collate:=True
Any suggestion regarding what I am doing wrong?
Your request goes beyond the advice we provide on this blog. If you have a specific question about the operation of a function or formula, I will try to answer it.
Is it possible to assign a macro button to a toolbar that can then be opened on any pc? It seems that when I forward my document with the macro buttons added to the toolbar, they disappear when the new user opens the document however the functions are still seen as listed macros in the document.
Hi! With a usual Excel file, you cannot transfer your toolbar settings to another user.
I need to run a macro to export a document to PDF, but I need to save to a different location each time I run it. Is this a possibility? I would ideally like to link this macro to a button.
Hello! To store the macros you use frequently, I recommend the Personal Macro Workbook. For more information, please visit: Personal Macro Workbook in Excel - make macros available in all workbooks .
How do I format the TEXT in a macro button, say BOLD, or FONT 14,,,etc
Right click/ edit text/ then just make the changes you want
Thank you for this useful post. I created a macro spreadsheet with keyboard shortcuts. I would like to ask if it is possible to convert those shortcuts into buttons in ribbon? And then have those shortcuts removed? ( I want to share the sheet with others, but don't want them to accidentally press those keys)
Yes, I found :)
Good Afternoon Svetlana,
I am trying to write two separate if statements with a nested LOOKUP so the responses in the relating cells don't return with a column heading nor an #N/A reply.
The first LOOKUP statement is as follows: =LOOKUP(2,1/($O$6:$O$19=Q2),$M$6:$M$19)
If the response is an amount, it lists the last amount, but if there isn't an amount -or the range is blank, I want it to reply with 0.00
The second LOOKUP statement is as follows: =LOOKUP(2,1/(K:K""),K:K)
The response is a date, but if there has not been a payment in the affecting range, I want it to reply with "No Payment Received"
Please help. Regards, Roger
Post a comment