Create a Macro Button in Excel
The most common tasks in Excel can be automated to avoid unnecessary time consumption. Having talked about automation VBA macros is the way to go forward in excel; once the macro coding is done, we need to execute that program to get the assigned job done. You must have already seen a button that can do the job in many of the worksheets, so once the programming is done, we can assign that work to one of the buttons, so by just click of the button, the assigned job will be executed. So in this article, we will take you through the process of creating an excel button to execute the task.
3 Methods to Create Button in Macro Excel?
As we know, macro coding is the perfect way to automate tasks; there is a small other thing we can do to execute that code. Macro & Buttons are the perfect combinations; it is like a “typical Indian marriage of boy likes a girl and girl likes a boy.” By just as the click of the button, we can execute hundreds of lines, so some of the complicated reports can be automated and hand over to your team members and say all they have to do is to click the button to get the job done.
So, in excel, we can create an excel button using three ways, and those are below.
- Active X Control
- Form Control in ExcelForm Control In ExcelExcel Form Controls are objects which can be inserted at any place in the worksheet to work with data and handle the data as specified. These controls are compatible with excel and can create a drop-down list in excel, list boxes, spinners, checkboxes, scroll bars.
We will see each one of them in detail now.
#1 – Using Shapes
The best and easy way to create a button to assign macro is using “Shapes” of Excel. We can insert a wide variety of shapes in excel, and also we can format these shapes as we would like to make them look.
Before inserting shapes, below is the macro we had written to filter values in the below data.
We have data in columns from A to E, and in G2 cell, we have created a drop-down list of all the card types, the card type that we choose in the G2 cell should be filtered out in the data and for this, below is the excel macro code we have written.
Sub Filter_Values() Dim LR As Long Dim LC As Long Dim Rng As Range LR = Cells(Rows.Count, 1).End(xlUp).Row LC = Cells(5, Columns.Count).End(xlToLeft).Column Set Rng = Cells(5, 1).Resize(LR, LC) Rng.AutoFilter Field:=4, Criteria1:=Range("G2").Value End Sub
Upon running the above code, it will filter the values which are selected in the cell G2 cell. Every time we need to open the Visual Basic EditorVisual Basic EditorThe Visual Basic for Applications Editor is a scripting interface. These scripts are primarily responsible for the creation and execution of macros in Microsoft software., and we need to run the code.
So, now we will insert shapes to assign the macro.
Follow the below given steps to assign macro to shape.
- Go to the INSERT tab in excelINSERT Tab In ExcelIn excel “INSERT” tab plays an important role in analyzing the data. Like all the other tabs in the ribbon INSERT tab offers its own features and tools. Under Insert Tab we have several other groups including tables, illustration, add-ins, charts, Power map, sparklines, filters, etc. and find the “Shapes” option.
- Once you click on the drop-down list of “Shapes,” we will see many shapes options. Select any of the shapes and draw on the worksheet.
- After adding the shapes, we need to enter some text value to it, so right-click on the shape and choose “Edit Text.”
- Enter text as you would like to convey the message to the user.
This is the default view of the shape.
- We can format the shape under the “Format” tab.
- Once the proper formatting is given to the shape right click and choose “Assign Macro.”
- Once the proper formatting is given to the shape right click and choose “Assign Macro.” Choose the macro that you would like to assign to the shape.
- Now click on “Ok,” and macro has been assigned to the shape.
Now when you hover on the shape, you can see the hand pointer indicating the click option. If you click, the assigned macro will be executed, and the value which is there in cell G2 will be filtered.
#2 – Using Form Control Shapes & Active X
Under the developer tabThe Developer TabEnabling the developer tab in excel can help the user perform various functions for VBA, Macros and Add-ins like importing and exporting XML, designing forms, etc. This tab is disabled by default on excel; thus, the user needs to enable it first from the options menu., we have an option called “Insert”; under this; we have “Form Control” and “Active X Control.”
From “Form Controls,” select the “Button” option.
Draw this on your worksheet, and immediately you will get a list of macros.
Choose the macro that you would like to assign and click on “Ok” to close the above window.
Now Button is created to run the macro.
Like this, we can create a button in excel to execute the macro.
Things to Remember
- The Active X Control button requires further coding knowledge, so not covered in this article.
- The basic difference between Shape & Form control is the formatting and style they look.
This has been a Guide to Create Button Macro in Excel. Here we discuss the Top 3 Methods using shapes, active X control, and form control along with examples and downloadable excel templates. You may learn more about excel from the following articles –