EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Tips VBA Tutorial for Beginners
Secondary Sidebar
VBA Tips
  • VBA Tips
    • VBA RGB
    • VBA Web Scraping
    • VBA AutoFill
    • VBA GoTo
    • VBA Color Index
    • VBA Wait
    • VBA Paste
    • VBA Copy Paste
    • VBA Remove Duplicates
    • VBA Sleep
    • VBA Font Color
    • VBA PowerPoint
    • VBA Borders
    • VBA Pivot Table
    • VBA Save As
    • VBA Tutorial For Beginners
    • VBA Charts
    • VBA Dictionary
    • VBA Conditional Formatting
    • VBA Paste Values
    • VBA Pause
    • VBA Refresh Pivot Table
    • VBA Macros
    • VBA Examples
    • Programming in Excel
    • VBA SendKeys
    • VBA Save Workbook
    • VBA PasteSpecial
    • VBA Function in Excel
    • VBA Visual Basic Applications
    • VBA Return

VBA Tutorial for Beginners

By Madhuri ThakurMadhuri Thakur

VBA Tutorial

Excel VBA Tutorial For Beginners

VBA or Visual Basic Applications is a part of Microsoft Excel. In Excel, we do our tasks manually or by formulas but the tasks which are repetitive needs to be done again and again. But by using VBA, we can make our jobs easier as we can make macros that will do our repetitive tasks for us. What is a macro again? Macro is a set of instructions that are given to do a specified task.

Basics of VBA

In this article, we will learn about the basics of VBA. It is the best VBA Tutorial for beginners. In VBA, we create macros, and as defined above, macros are a set of instructions that are given to do a specific task. There are two ways to give these instructions for macros. One is through writing codes in VBA itself, while another is done by recording a macro. In excel, we have a special feature in which we record our steps, and excel does the same steps for us over and over when giving excel command to run that code. But it has certain limitations to it, so we write codes in VB Editor where there is an ultimate number of opportunities to automate our tasks.

VBA is basically a programming language just like C and Java and everything else. Here we record a set of instructions by writing some block of codes, and when we run these blocks of codes, they do the specified tasks for us. By default, VBA is not accessible in excel. To access VBA, we need to follow some steps first, as listed below.

As VBA is different from the normal worksheet we work upon, it contains macros, so saving it is different from other normal workbooks; when we save a workbook that contains macros, we need to save it as a macro-enabled workbook to ensure that our macros are saved in the workbook.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

If you don’t see the developer tab in your excel, follow the below steps to enable the developer tab in excel.

  • Go to the File tab on the home page of excel.

VBA Tutorial Step 1

  • In the files tab, we will find a section for options in the last from the bottom; click on Options.

VBA Tutorial Step 2

  • It will open another wizard box for us as Excel Options. In excel options, click on Customize Ribbon.

Customize Ribbon

  • When we drag down in the customize ribbon options, we will find an option for Developer; we need to check that box which will enable us to use VBA in excel.

VBA Tutorial Step 4

  • Now we have the developer’s tab enabled in excel as follows.

Developer’s tab

How to Use VBA in Excel?

There are two methods for using VBA in Excel.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,142 ratings)
  1. One is by recording macros.
  2. The second is by writing a block of codes.

The most advisable thing to do is learn by recording the macro and look at the code to learn the syntax, which will help us in writing the codes of our own.

How to Record Macros in Excel VBA?

In this Excel VBA tutorial for beginners, before we move to learn how to write codes in VBA, let us start with the most basic function of VBA, which is to record a macro. Recording macro is recording steps we perform and let excel do it over and over when we give the command to do it. Let us learn all these in examples which will help us to understand it better.

You can download this VBA Tutorial Excel Template here – VBA Tutorial Excel Template
VBA Tutorial – Example #1

Let us record our first macro. For this example, I already have some data in sheet one as follows.

VBA Tutorial Example 1-1

We have marks of some students and have a formula in column E that if the percentage is above 35%, they will declare as pass or as fail. Currently, there is no data in column D, so by default E column value is false. Let us do this by recording a macro.

  • In the View tab, we have a Macros section click on it.

VBA Tutorial Example 1-2

  • We get three options, select Record Macro.

Record Macro

  • This will open a wizard box for us somewhat like this,

VBA Tutorial Example 1-4

  • Always remember that a macro name cannot have spaces, give a name to macro and a shortcut key to run it. The other boxes are optional.

VBA Tutorial Example 1-5

Press ok, and the macro records every step, every cell we select or every click we make.

  • Let us calculate the percentage for the given an example as follows,

VBA Tutorial Example 1-6

  • Once we have completed our task now, we need to stop recording a macro. In the same macro tab from the Views table, we have another option to Stop Recording. Click on it to stop recording a macro.

Stop Recording

  • Now to test it, remove the values from the percentage section and leave it blank.

VBA Tutorial Example 1-8

  • Press CTRL+J as CTRL+J was the shortcut for our macro and see the result.

VBA Tutorial Example 1-9

We have given this set of instructions to excel not by code, but excel recorded every step we followed and made an own code by itself. We can view the code too. In the macros section, click on View macro, and we will have a list of macros if we made any on that workbook as follows.

VBA Tutorial Example 1-10

Click on Edit, and it will take us to the code window for this macro, as shown below.

Code Window

We did not write this code but excel recorded our steps and transformed them into codes. This is a very unique feature of excel, but it has some limitations like; if we do an extra step in excel, we record the step and execute it every time we run the macro.

How to Make Simple Macros in Excel Using VBA code?

Below is the Excel VBA tutorial example to make our first sample macro in VBA by writing code by ourselves. To write a code in VBA, we must know this every macro starts and ends with SUB () function.

VBA Tutorial – Example #2
  • In the Developer’s tab, click on Visual Basic, which will open VB Editor for us, where we will write our first macro.

VBA Tutorial Example 2-1

  • Once the VB Editor is open, we can see many windows around us. On the left-hand side, we have a project window which keeps records for our modules where we write codes and properties of a VBA project; also, we have a toolbar above with various ranges of tools to use.

VBA Tutorial Example 2-2

  • To write a VBA code or macro, we need a platform or a code window to write it. We write codes in modules. Modules can be accessed from the Insert tab.

VBA Tutorial Example 2-3

  • When we click on Module and double click on it, then another window opens for us, which is our platform to write code. We first name our macro as follows.

Code:

Sub MyMacro()

End Sub

VBA Tutorial Example 2-4

  • When we want to work with variables, we need to declare these variables with data types like in every other programming language such as follows. We use the Dim keyword to declare our variables.

Code:

Sub MyMacro()

Dim A, B, C As Integer

End Sub

Declare variables

  • We can perform any task with these variables such as follows,

Code:

Sub MyMacro()

Dim A, B, C As Integer
A = 10
B = 15
C = A + B

End Sub

VBA Tutorial Example 2-6

  • To display a value in VBA, we use Msgbox Function as shown in the image below.

Code:

Sub MyMacro()

Dim A, B, C As Integer
A = 10
B = 15
C = A + B
MsgBox C

End Sub

Msgbox Function Example 2-7

  • Run the code by hitting the F5 or Run button and see the output.

Run the Macro

Things to Remember

  • VBA is not enabled by default in Excel; it has to be manually enabled.
  • Not two macros can have the same name.
  • Macros names cannot have spaces among them.
  • When we record a macro, it records every step we perform, so we need to be careful with every step we take.

Recommended Articles

This is a guide to Excel VBA Tutorial For Beginners. Here we discuss the steps to enable the developer tab and learn how to record macros and also learn to make sample macros in Excel VBA along with practical examples and downloadable excel template. Below are some useful excel articles related to VBA –

  1. VBA Call Sub
  2. VBA Web Scraping
  3. VBA Clear Contents
  4. VBA XML
Popular Course in this category
VBA Training (4 Courses, 12+ Projects)
  4 Online Courses |  13 Hands-on Projects |  50+ Hours |  Verifiable Certificate of Completion
4.7
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more