how to add button in excel
Bottom Line: Learn how to create macro buttons in a worksheet that run VBA code when clicked. Includes how to format buttons and prevent them from moving and resizing on the sheet.
Skill Level: Intermediate
Download the Excel File
Here's the Excel workbook that I use in the video so you can follow along and practice what you're learning.
Create Macro Buttons.xlsm (33.1 KB)
Making User-Friendly Macro Buttons in Your Spreadsheets
Macros make life easier. Buttons make life easier. It's only natural to marry the two and make the processes you repeatedly have to perform as simple as possible.
You can literally run through a hundred steps with just one small click using macros and buttons–which, of course, is both empowering and gratifying.
Kristen, a member of our VBA Pro Course, loves to add buttons to her spreadsheets for this reason.
You can watch the entire video to learn more about Kristen's story with learning VBA, and how she uses macro buttons.
For this tutorial, we're using macros that have already been created. If you don't know how to create macros yet, then checkout one of my free webinars to learn the basics getting started with macros & VBA.
So let's take a look at how to create a button and assign a macro to it.
3 Types of Macro Buttons for Worksheets
In this post I explain three different types of buttons you can place on a worksheet.
- Shapes
- Form Control Buttons
- ActiveX Control Buttons
We'll start with my favorite…
1. Using Shapes to Create Macro Buttons
In my opinion, the best way to create macro buttons is to use shapes. Shapes give us the most styling/formatting options, and we can make them look like buttons you click every day on modern web pages.
Below, we will create the button on a worksheet and assign a macro to it. Here are the steps to create the macro button:
1. Draw a shape on the sheet (Insert tab > Shapes drop-down > Rectangle shape).
2. Add text to the shape (Right-click > Edit Text | or double-click in the shape).
3. Assign the macro (Right-click the border of the shape > Assign Macro…)
4. Select the macro from the list. It's important to note that the macro should be selected from the This Workbook list so that when you share the workbook with other users, the macro will be shared as well.
If you assign the macro from another workbook, the button will not work for the people you share the workbook with.
5. Press OK.
When you hover your mouse over the shape it will now turn into the hand pointer cursor. Clicking the shape will run the macro.
That's it! That's how you assign a macro to a button that you've created right on the worksheet.
Modifying the Button
If you need to select the shape to change its look or move it, hold the Ctrl key while clicking the shape. This will prevent the macro from running and allow you to modify the shape. Excel offers a ton of options on the Format tab for changing the shape style, shape effects, font color or size, etc.
Preventing the Button from Resizing with Cell Changes
Depending on your settings, it's likely that when you change the size of the column or row that your button is on, the button size will change as well. If you don't want that to happen, you can easily adjust the settings to keep the button static.
First, just right click on the button and choose Size and Properties…
This will bring up the Format Shape pane. There are options for you to choose from in the Properties section, depending on whether or not you want the button to move or resize when changes are made to your sheet.
2. Using Form Controls to Create Macro Buttons
The next option for macro buttons is Form Controls. They are essentially the same in terms of set-up.
The difference between Controls and Shapes is the styling.
Form Control buttons do give the appearance of being pressed when you click on them, which is kind of cool. However, they unfortunately look like a button you'd find on an early version of Windows.
We cannot change the color of the button either. The only formatting options are for the font type, size, and color.
Insert a Form Control Button
To add a Form Control button, just go to the Developer tab and click on the Insert button. A drop-down gives you options under Form Controls.
3. Using ActiveX Controls for Macro Buttons
The last option is ActiveX Control buttons.
The ActiveX Control buttons do have more formatting options and you can change the button color.
They also require you to add an event macro in the sheet module that will run when the button is clicked. This is done by right-clicking the button and choosing View Code.
You can add other event triggers like double-click. This could perform a different action/macro when the button is double-clicked.
However, I don't recommend the ActiveX Control buttons. In my experience they are glitchy in terms of consistent appearance. They can end up really big or really small when changes are made to the sheet or when the file is shared back and forth between users.
Placing Buttons in the Ribbon or Toolbar Instead
Another option is to place your macro buttons on any of the tabs in the Excel Ribbon or Quick Access Toolbar.
This is video #3 in my 4-part video series on the Personal Macro Workbook. This technique is best for macros that you will only run on your computer because the ribbon button customizations do not travel with the workbook.
If you do want the ribbon buttons to travel with the workbook then you can use XML code to customize the ribbon. I'll write an article on that in the future, and I do cover extensively in my VBA Pro Course, along with how to create add-ins.
Related Tutorials
I have a couple other posts for you to check out on this topic. Take a few minutes to give them a look!
- How to Create VBA Macro Buttons for Filters in Excel
- Macro Buttons to Add Fields to Pivot Tables
- How to Add a Yes No Pop-up Message Box to a Macro Before It Runs
- How to Enable the Developer Tab in Excel
Conclusion
My overall recommendation is to keep it simple and stick to using shapes for macro buttons. They give you the most formatting options, and are easy to setup and modify.
What will you create macro buttons for? I'd love to hear how you plan to put these buttons to work. If you have any comments or questions about running a macro button that you've created, please leave a comment below.
Thank you! 🙂
how to add button in excel
Source: https://www.excelcampus.com/vba/macro-buttons/
Posted by: johnsonwousidersing.blogspot.com
0 Response to "how to add button in excel"
Post a Comment