www.himset.com

facebook Instagram Twitter You Tube

How to create button in excel worksheet using Macros

When you write macros or vba codes in excel, so many times you need a method to run your macro or to activate your code to perform a specific task. For example you wrote a piece of code to calculate average of student’s marks and want to run this macro. How you can do that, generally Macros or vba code does not run by default, you need a link or object to run this code.

We make a link or object by inserting a button in worksheet at your convenient place. To run a macro just click on a button and it will perform a task for us. We will understand the procedure of inserting a button in worksheet step by step below:

Fig:-1 Insert Shape to worksheet

Insert shape to worksheet

To make a button, Insert Shape to worksheet as shown in fig:-1 by drage and drop method. You can choose any type of shape it is up to you. Adjust the size of shape.

Fig:-2 Add text to shape

Add Text to Shape

To add text to shape right click on the shap and you will see the dialog as shown in fig 2. Click on edit text and write the button text from keyboard.

Fig:-3 Create a Macro and write small code to test the button.

Create a Macro

To make a button from shape we have to assign macro to it but firstly we need to create a macro as shown in fig 3. To open this dialog press alt+f8 or click on view tab at top menu bar then click on macros icon. In the text box type macro name. You can write any name as you like just like I wrote MacroButton, then press create button as shown in fig 3.

Fig:-4 Write a small piece of code in between Sub MacroButton()

Macro Code

Write this small example piece of code as shown in fig-4 to calculate average of student’s marks and save the sheet as type “Excel Macro- Enabled worksheet” then go to main worksheet and right click on shape and press Assign Macro menu as shown in fig-2.

After opening the window select macro name from list you want to assign to the shape. In our case select MacroButton. After assigning the macro to the shape it will become button and when you click this button it will calculate the average of student’s marks. You can do more experiments with buttons. Happy learning.



Excel Macro & VBA