What is VBA Macro in Excel?
A VBA Macro is nothing but a line of code to instruct the excel to do a specific task. Once the code is written in VBE you can execute the same task any time in the workbook. The macro code can eliminate repetitive boring tasks and automate the process.
If you are new to VBA and you don’t know anything about it, then this is the article to start off your journey in macros. Ok, let’s 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. All the programming we wish to do will be done in the VBE (Visual Basic Editor). It is the platform to write our code of task to execute in excel. To start off with VBA coding in excel, you need to record a macro.
Enable Developer Tab in Excel
VBA coding is available under the Developer tab in excel.
If you don’t see this developer tab in your excel, follow the below steps to enable the developer tab in excel.
Step 1: Go to File.
Step 2: Under File, go to Options.
Step 3: Select Customize Ribbon.
You will see the Excel Options window from that select Customize Ribbon Option.
Step 4: Check the box Developer Tab to enable it.
Step 5: Click on OK to enable it.
Now you should see the Developer tab.
How to Record Macros in Excel VBA?
Ok, we will start straight away by recording the Macro. Under the Developer tab, click on Record Macro.
As soon as you click on the Record Macro, you will see excel asks you to give a name to your macro.
Give a proper name to the macro. It should not contain any space characters and special characters. You can give underscore (_) as the word separator then clicks on OK to start the recording.
From now onwards, the macro recorder keeps recording all the activities you do in the excel sheet.
Firstly I will select cell A1.
Now I will type “Welcome to VBA” in A1 cell.
Now I will stop recording under the developer tab.
So, excel stops recording the activities we do in the excel. Now let’s see how excel recorded the activities. Under the Developer tab, click on Visual Basic.
As soon as you click on Visual basic, we will see the below window.
4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
Double Click on Modules.
Now we will see the code on the right-hand side.
Macro code started with the word SUB.
All the macro has two parts to it one is Head, and another one is Tail. Every macro has a name.
In between the head and tail of the macro, excel recorded all the activities.
The first thing we did after start recording the macro is we have selected the cell A1 and excel recorded it as Range (“A1”).Select
The second activity was we have entered the value “Welcome to VBA.” As soon as we selected, then it becomes an active cell, so excel recorded the activity as ActiveCell.FormulaR1C1 = “Welcome to VBA.”
A third activity is after typing the word “Welcome to VBA,” we hit enter, and excel selected A2 cell.
Like this, Macro Recorder recorded all the activities we have done in the excel sheet. Now delete the word in cell A1.
After deleting the word, once again, go to VBE where our code is. There click on the Run button to once again enter the same text value to cell A1.
So macro is executed, and we got the same value again. Like this, we can automate our daily routine work to save a lot of time and get rid of boring tasks every day.
Example #2 –
Now let’s 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.
Click on Ok to start the recording. I will enter 1, 2, 3, and then I will drag the fill handle to inert serial numbers.
Using Fill Handle, I am inserting serial numbers.
Now click on stop Recording.
Go to Visual Basic Editor and see what the code is.
Let’s look at the code now. Firstly we have selected cell A1.
Secondly, we have inserted 1 to the active cell.
ActiveCell.FormulaR1C1 = "1"
The third activity was we have selected the cell A2.
The fourth activity was we have inserted 2 to the active cell.
ActiveCell.FormulaR1C1 = "2"
The fifth activity was we have selected the cell A3.
The sixth activity as we have inserted 3 to the active cell.
ActiveCell.FormulaR1C1 = "3"
Then we have selected the range of cells from A1 to A3.
After selecting the cells, we have 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.
So, now whenever we want to insert serial numbers from 1 to 10 in cell A1 to A10, you can run this code.
How to Save Macro Workbook?
Excel workbook, which contains macro code, should be saved as Macro-Enabled Workbook. Click on Save As and select the extension of the file as “Macro-Enabled Workbook.”
Things to Remember
- This is just the introduction part of the VBA Macro Tutorial. Keep following our blog to see more posts going forward.
- Recording macro is the best initialization to start the journey of macros.
- Record more and more activities and see what the code is.
This has been a guide to VBA Macro. Here we learn how to record a macro in excel to show automatically generated code in VBA editor along with step by step examples. Below are some useful excel articles related to VBA –