WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA Tutorial

VBA Tutorial

Excel VBA Tutorial for Beginners

If you are new to VBA and you don’t know anything about it, then this is the best tutorial for beginners to start off their journey in Excel VBA macros. Ok, let’s start the journey of your VBA tutorials today.

VBA is 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). VBE is the platform to write our code of task to execute in excel.

vba tutorial excel

What are the Macros in Excel?

A 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 at any time in the workbook.

The macro code can eliminate repetitive, boring tasks, and automate the process. To start off with the VBA Macro coding technique, let’s record the macro.

VBA coding is available under the DEVELOPER tab in excel.

VBA Tutorial (Developer tab) 1

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 -> Under FILE, go to OPTIONS.

vba msg box

Step 2: Select Customize Ribbon

You will see the Excel Options window. Select Customize Ribbon in Excel.

msgbox 1

Step 3:Check the box DEVELOPER to enable it.

msgbox 2

Step 4: Click on OK to enable it.

Now you should see the Developer tab.

vba msgbox 3

Tutorial to Record Macros in VBA Excel

In this tutorial about VBA, we will learn how to record macros in Excel with practical examples.

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

Example #1

Ok, we will start straight away by recoding the Marco.

Step 1: Click on Record Macro

Under developer, tab clicks on Record Macro.

(Record Macro)

Step 2: Give Name to Macro

As soon as you click on the Record Macro, you will see excel asks you to give a name to your macro.

Tutorial (Record Macro) 1

Give a proper name to macro. The macro should not contain any space characters and special characters. You can give underscore (_) as the word separator.

VBA Tutorial (Record Macro) 2

Step 3: Click 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.

VBA Tutorial (Select A1)

Now I will type “Welcome to VBA” in A1 cell.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

VBA Tutorial (welcome to vba)

Step 4: Stop Recording

Now I will click on the stop recording option under the developer tab to stop the recording.

VBA Tutorial (Stop recording)

So, excel stops recording the activities we do in the excel. Now let’s see how excel recorded the activities.

Step 5: Open VBA Editor

Under the Developer, tab click on Visual Basic.

VBA Tutorial (Visual Basic)

As soon as you click on Visual basic, we will see below the window.

VBA Tutorial (Visual Basic) 1

Double Click on Modules.

VBA Tutorial (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.

VBA Tutorial (head & Tail)

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.”

Note: R1C1 is row 1, column 1.

The third activity is after typing the word “Welcome to VBA,” we hit enter, and excel selected A2 cell. So excel recorded as the activity as Range (“A2”). Select

Like this, Macro Recorder recorded all the activities we have done in the excel sheet. Now delete the word in cell A1.

VBA Tutorial (Delete the word)

After deleting the word, once again, go to VBE where our code is.

Step 6: Run the Code

There click on the RUN button to once again enter the same text value to cell A1.

VBA Tutorial (run the code)

Note: Shortcut key to run the code is F5.

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.

Example 2

Click on Ok to start the recording. I will enter 1, 2, 3. Then I will drag the fill handle to inert serial numbers.

Example 2-1

Now click on Stop Recording.

Stop recording

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

Example 2-2

Let’s look at the code now.

Firstly we have selected cell A1.

Code:

Range (“A1”).Select

Secondly, we have inserted 1 to the active cell.

Code:

ActiveCell.FormulaR1C1 = "1"

The third activity was we have selected the cell A2.

Code:

Range (“A2”).Select

The fourth activity was we have inserted 2 to the active cell.

Code:

ActiveCell.FormulaR1C1 = "2"

The fifth activity was we have selected the cell A3.

Code:

Range (“A3”).Select

The sixth activity as we have inserted 3 to the active cell.

Code:

ActiveCell.FormulaR1C1 = "3"

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

Code:

Range ("A1:A3").Select

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

Code:

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

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

Code:

Range ("A1:A10").Select

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 in excel and select the extension of the file as “Macro-Enabled Workbook.”

VBA Tutorial Example 2-3

Things to Remember

  • This is just the introduction part of VBA. 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.

Recommended Articles

This has been a guide to VBA Tutorial for beginners. Here we discuss steps to enable the developer tab and also learn how to record macros in Excel VBA along with practical examples. Below you can find some useful excel VBA articles –

  • VBA Font Color Examples
  • Pivot Table using VBA Code
  • How to Paste in VBA?
  • How to Select Cell in VBA?
17 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download VBA Macro Tutorial Excel Template

New Year Offer - VBA Training Course (6 courses, 35+ hours video) View More