Create a Macro in Excel
Create macros using Excel VBA to automate repetitive tasks that improve your Efficiency in Microsoft Excel. In this tutorial, you will learn to make a simple macro that runs when you click a button. First, turn on the Developer tab.
Developer Tab
Follow these steps to enable the Developer tab and access advanced tools in Excel.
1. Right-click on the ribbon, choose Customize Ribbon to adjust tab layout.
2. In the Customize the Ribbon section on the right side of the dialog box, choose Main Tabs if needed.
3. Check the Developer check box.
4. Click OK.
5. The Developer tab is located beside the View tab.
Command Button
A Command Button is a clickable control used to run a specific action or macro. Use the following steps to insert a command button:
1. On the Developer tab, click Insert.
2. Click the Command Button under ActiveX Controls.
3. You can drag the Command Button anywhere in the sheet.
Assign a Macro
Please follow the steps below to attach a macro to the command button.
1. Right-click on CommandButton1 when Design Mode is enabled to access its properties and settings.
2. Click View Code.
The Visual Basic Editor appears.
3. Move your cursor inside the code between “Private Sub CommandButton1_Click()” and “End Sub” lines.
4. Add the code line shown below.
Note: In the Project Explorer, you will see Sheet1 and This Workbook listed. Click on Sheet1 in Project to see its Visual Basic code. Go to View and click Project Explorer if it isn’t already open. Ignore the Option Explicit statement for the time being.
5. Close the Visual Basic Editor.
6. Click the command button in the worksheet, but first check that Design Mode is not selected.
Result:
Congratulations. You’ve just created a macro in Excel!
Visual Basic Editor
The Visual Basic Editor is a built-in tool in Excel used to write, edit, and manage VBA code for automating tasks and customizing Excel functions. To open the VB editor, go to Developer and click Visual Basic.
The Visual Basic Editor appears.
1/9 Completed! Learn more about creating macros.➝
Next Chapter: MsgBox