Create a macro in Excel

Create a Macro in Excel

MS Excel supports VBA to automate tasks in MS Excel by writing macros. In this tutorial, you will learn how to create a simple macro that can be executed by clicking a command button. But first, you will need to turn on the Developer tab.

Enabling Developer Tab

To turn on the Developer tab, follow the steps below.

1. Right click on the ribbon at any place, and then click Customize the Ribbon as shown in the figure below.

Customize the Ribbon

2. On the right section of Customize the Ribbon window, select Main tabs (if necessary).

3. Tick the Developer check box.

Developer check box

4. Click OK.

5. Developer tab will be added to your MS Excel ribbon.

MS Excel ribbon

Command Button

To place a command button on a worksheet, follow the steps below.

1. On the Developer tab, click Insert.

2. In the ActiveX Controls group, click Command Button.

ActiveX

3. Drag a command button on your worksheet or simply click the button and draw on the desired place of the worksheet.

command button

Assign a Macro

To assign a macro to a command button, follow the steps below.

1. Select the Design Mode from the Developer tab

Design Mode

2. Right click CommandButton1 and select view code.

view code

Following Visual Basic Editor will appear.

Visual Basic Editor

3. Place your cursor between Private Sub CommandButton1_Click() and End Sub.

4. Add the code line shown below.

Add the code

5. Close the Visual Basic Editor.

7. Deselect the Design Mode

6. Click the command button on the sheet

Result:

Result

Excellent. You’ve just created your first macro in MS Excel!