VBA Macros

Updated on June 11, 2024
Article byWallstreetmojo Team
Edited byAshish Kumar Srivastav
Reviewed byDheeraj Vaidya, CFA, FRM

What is VBA Macro in Excel?

A VBA Macro is nothing but a line of code to instruct Excel to do a specific task. Once we write the code in VBA, we can execute the same task at any time in the workbook. The macro code can eliminate repetitive, boring tasks and automate the process.

If you are new to VBA and do not know anything about it, this is the article to start your journey in Macros. So, let us start the journey of your coding class today.

VBA Visual Basic for Applications is the Microsoft programming language for Microsoft products like Excel, Word, and PowerPoint. It will do all the programming we wish to do in the VBE (Visual Basic Editor). It is the platform to write our code of tasks to execute in Excel. To start with VBA coding in Excel, you need to record a Macro.


You are free to use this image on your website, templates, etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA Macros (wallstreetmojo.com)

–>> If you want to learn Excel VBA professionally, then our VBA Basic Course (16+ hours) is the perfect solution. In our Basic Excel VBA course you learn the skill of automating tasks using Variables, Data Types, Ranges, and Cells in VBA. Master Control Structures, including Conditional Statements and Loops, and discover techniques for manipulating data through sorting, filtering, and formatting. By the end of the course, you will be able to apply your acquired skills to real projects, culminating in an Excel VBA project which will solidify your ability to create efficient, automated solutions.

Enable Developer Tab in Excel

VBA coding is available under the Developer tab in excel.

If you do not see this developer tab in your Excel, follow the below steps to enable the Developer tab in Excel.

Note: We are using Excel 2016 version.

Step 1: Go to File.

Developer tab Step 1

Step 2: Under File, go to Options.

Developer tab Step 2

Step 3: Select Customize Ribbon.

You will see the Excel “Options” window from that select Customize Ribbon option.

Developer tab Step 3

Step 4: Check the box Developer Tab to enable it.

Developer tab Step 4

Step 5: Click on OK to enable it.

Now you should see the Developer tab.

Developer tab Step 5

How to Record Macros in Excel VBA?

You can download this VBA Macro Excel Template here – VBA Macro Excel Template

Example #1

We will start straight away by recording the Macro. Then, under the “Developer” tab, click on Record Macro.

VBA Macro Example 1

As soon as you click on the “Record Macro,” Excel asks you to give a name to your Macro.

VBA Macro Example 1-1

Give a proper name to the Macro. It should not contain any space characters or special characters. For example, you can give underscore (_) as the word separator and then click on “OK” to start the recording.

VBA Macro Example 1-2

From now onwards, the macro recorder keeps recording all your activities in the Excel sheet.

Firstly, we will select cell A1.

VBA Macro Example 1-3

Now, we will type “Welcome to VBA” in the A1 cell.

VBA Macro Example 1-4

Now, we will stop recording under the “Developer” tab.

VBA Macro Example 1-5

So, Excel stops recording the activities we do in Excel. Now, let us see how Excel recorded the activities. Under the “Developer” tab, click on “Visual Basic.”

VBA Macro Example 1-6

As soon as you click on “Visual Basic,” we will see the below window.

VBA Macro Example 1-7

Double click on “Modules.”

VBA Macro Example 1-8

Now, we will see the code on the right-hand side.

VBA Macro Example 1-9

Macro code started with the word SUB.

All the macro has two parts: the “Head” and the “Tail” because every macro has a name.

VBA Macro Example 1-10

In between the head and tail of the macro, excel recorded all the activities.

The first thing we did after starting recording the macro was we selected cell A1, and Excel recorded it as Range (“A1”). Select.

The second activity was when we entered the value “Welcome to VBA.” When we selected it, it became an active cell, so Excel recorded the activity as ActiveCell.FormulaR1C1 = “Welcome to VBA.”

Note: R1C1 is row 1, column 1.

A third activity is after typing the word “Welcome to VBA,” we press the “Enter” key, and Excel selects the A2 cell.

Like this, the Macro recorder recorded all our activities in the Excel sheet. Now, delete the word in cell A1.

VBA Macro Example 1-11

After deleting the word, go to VBE, where our code is once again. Click on the “Run” button to enter the same text value to cell A1.

Note: The shortcut key to run the code is F5.
VBA Macro Example 1-12

So the macro is executed, and we get the same value again. Like this, we can automate our daily routine work to save time and eliminate boring daily tasks.

Example #2 –

Now, let us record one more macro to understand better. In this recording, we will insert serial numbers from A1 to A10.

Go to the “Developer” tab and click on the “Record Macro” option, as shown in the above example.

Record Example 2

Click on “OK” to start the recording. First, we will enter 1, 2, 3, and then drag the fill handle to insert serial numbers.

Using “Fill Handle,” we will insert serial numbers.

Example 2-1

Now, click on “Stop Recording.”

VBA Macro Example 2-2

Go to “Visual Basic Editor” and see what the code is.

Example 2-3

Let us look at the code now. Firstly, we have selected cell A1.


Range (“A1”).Select

Secondly, we have inserted 1 into the active cell.


ActiveCell.FormulaR1C1 = "1"

The third activity was when we selected cell A2.


Range (“A2”).Select

The fourth activity was we inserted 2 into the active cell.


ActiveCell.FormulaR1C1 = "2"

The fifth activity was when we selected cell A3.


Range (“A3”).Select

In the sixth activity we inserted 3 into the active cell.


ActiveCell.FormulaR1C1 = "3"

Then, we selected the range of cells from A1 to A3.


Range ("A1:A3").Select

After selecting the cells, we filled the serial numbers using the fill handle.


Selection.AutoFill Destination:=Range("A1:A10"), Type:=xlFillDefault

So finally, we have selected the range A1 to A10.


Range ("A1:A10").Select

So, you can run this code whenever we want to insert serial numbers from 1 to 10 in cells A1 to A10.

How to Save a Macro Workbook?

One must save the Excel workbook containing macro code as “Macro-Enabled Workbook.” Then, click “Save As” and select the file’s extension as “Macro-Enabled Workbook.”

VBA Macro Example 2-4

Things to Remember

  • It is just the introduction part of the VBA Macro tutorial. Keep following our blog to see more posts in the future.
  • Recording macro is the best initialization to start the journey of macros.
  • Record more and more activities and see what the code is.

Recommended Articles

This article has been a guide to VBA Macro. Here, we learn how to record a macro in Excel to show automatically generated code in the VBA Editor, along with step-by-step examples. Below are some useful Excel articles related to VBA: –