In this tutorial, we are going to explain you the Basics of a VBA Macro in the Microsoft Excel and step-by-step guide to create your first VBA Macro.
What is a Macro?
A series of commands or instructions that are combined to form a single command are known as Macros. Macros are getting created by writing the VBA code with the set of instruction in the Visual Basic Editor of Microsoft Excel.
Benefits of a Macro
Macros can save your time by letting you automate relatively simple tasks that you need to perform often, as well as complex procedures that consist of many steps.
In this tutorial, you will learn how to create a simple macro which will be
executed after clicking on a command button. To do so, First, turn on the
Developer tab.
Click on the below respective link to know, how to Turn On the Developer Tab or Insert Command Button or Assign a Macro to the command button or how to open Visual Basic Editor
in the Microsoft Excel.
Developer Tab | Command Button | Assign a Macro | Visual Basic Editor
Developer Tab:
To turn on the Developer tab, execute the following steps.
-
Right click anywhere on the ribbon, and then click Customize the Ribbon
(as shown in the below screenshot).
Alternatively, you can go to File Menu then Click on Options and then click on Customize the Ribbon option shown in the left pane of the Excel Options window, the same window will open as shown you in the below image.
Customize Ribbon
- Under Customize the Ribbon, on the right side of the dialog box, select Main tabs (if necessary).
-
Check the Developer check box.
Turn On Developer Tab
- Click OK.
-
You can find the Developer tab next to the View tab.
Developer Tab
Command Button:
In this tutorial we are going to explain how you can Insert the Command Button in the Microsoft Excel.- In the Developer tab, click on Insert option.
-
In the ActiveX Controls group, click on Command Button.
Insert Command Button Control
- Drag a command button on your worksheet.
Assign a Macro:
In this tutorial we are going to explain how you can Assign a Macro to the Command Button in the Microsoft Excel.To assign a macro (one or more code lines) to the command button, execute the following steps:
- Right click CommandButton1 (make sure Design Mode is selected).
-
Click View Code.
The Visual Basic Editor appears.
View Code
- Place your cursor between Private Sub CommandButton1_Click() and End Sub.
- Add the code line shown below:
Range("A1").Value = "Hello"
Note: The window on the left with the name Sheet1 (Sheet1) and ThisWorkbook is called the Project Explorer. If the Project Explorer is not visible, click on View menu and then click on Project Explorer. If the Code window for Sheet1 is not visible, click Sheet1 (Sheet1). You can ignore the Option Explicit statement for now.
Add Code Line
- Close the Visual Basic Editor.
- Click the command button on the sheet (make sure Design Mode is deselected).
Result:
|
|
Macro Result |
Visual Basic Editor
In this tutorial we are going to explain how you can open the Visual Basic Editor in the Microsoft Excel.To open the Visual Basic Editor, Go to the Developer tab and then click on Visual Basic.
|
|
Open Visual Basic Editor |
|
|
Visual Basic Editor |



No comments:
Post a Comment