Excel VBA: The Best Guide to Learn

VBA stands for Visual Basic Analysis. Excel VBA is Microsoft’s programming language for Office applications such as MS-Excel, MS-Word, and MS-Access. Macros are what most people who write VBA code use.

What Is VBA?

Visual Basic for Application is a human-readable and editable programming code that gets generated when you record a macro. Today, it is widely-used with other Microsoft Office applications such as MS-Word, MS-Excel, and MS-Access. 

Business Analyst Master's Program

Gain expertise in Business analytics toolsExplore Program
Business Analyst Master's Program

Enable Developer Option in Excel

It hides the Developer tab on the ribbon by default. To customize the ribbon, follow the steps mentioned below:

  • Right-click on the ribbon (anywhere) click on the Customize the Ribbon option.

developer

  • Go to Customize the Ribbon and select the Developer checkbox.

ribbon

VBA Editor Interface

You can open the VBA interface by using the ALT + F11 keyboard shortcut, or you can go to the Developer tab and click on Visual Basic.

vba editor

Create an Excel Macro using a Command Button

Create a Command Button

Now that you have enabled the developer tab and have some familiarity with the VBA editor, let’s start creating a macro by using a command button.

To place a command button on your worksheet, follow these steps:

  • Go the Developer tab > Insert > ActiveX Controls > Command button.

record macros

  • Drag the command button on your worksheet.

record macros

Assigning a Macro to a Command Button

To assign a macro to the command button, follow these steps:

  • Right-click on the command buttons and select View Code. 

record macros

  • Add the following lines of code shown below.

record macros

  • Close the VBA editor and click on the command button on the worksheet. Make sure to deselect the design mode.

macro

Create a Message Box

The MsgBox is a dialog box in Excel that will prompt a message on your worksheet. To create a MsgBox, add the following lines of code to the command button.

excel vba

Now, when you click on the button, you will get the following message.

msgbox

Create an Input Box

The InputBox function prompts the user to enter the values and returns the information entered in the dialog box. 

Syntax: 

InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile,context])

To add the InputBox to your command button, execute the following steps:

  • Declare the variable name and keep the type variant. A variant variable can hold any type of value.
  • Add the following code to show the input box.

inputbox

  • Now when you click on the command button, you will get a prompt asking for a value.

  • Enter your input and click OK. It will enter your input on cell A1.

Variables, Constant and Operators in VBA

Variables

Variables are the data types that are used to hold a value. We can change the variables during the execution of the program.

Syntax: Dim <<variable_name>> As <<variable_type>>

We can divide the VBA data types into two categories:

  • Numeric Data Types: Numeric data types consist of byte, integer, long, single, double, currency, and decimal. 
  • Non-Numeric Data Types: Non-numeric data types consist of string, date, boolean, object, and variant.

excel vba

Constants

Constants are the fixed value that cannot be changed during the execution of the program.

Syntax: 

Const <<constant_name>> As <<constant_type>> = <<constant_value>>

Example:

constant

When you click on the command button, you will get the following output:

constant 2

FREE Course: Introduction to Data Analytics

Mastery to Data Analytics Basics is a Click Away!Start Learning
FREE Course: Introduction to Data Analytics

String Manipulation

String manipulation refers to the process of analyzing, manipulating, and effectively handling string values.

Join Strings

You can join the two strings by using & operator. This is also known as concatenation.

join strings

Click on the command button to get the following result.

join-strings-2

Left

The left keyword will extract the characters from the leftmost side of the string.

lest 1

excel vba

If, If-Else, For and While Loop

If statement

The ‘If statement’ is a conditional statement that consists of an expression followed by another expression. If the condition is true, the lines of code under the If statement are executed.

Syntax: 

If(boolean_expression) Then

   Statement 1

   .....

   .....

   Statement n

End If

Example:

if excel

Enter the marks in cell A1 and click on the command button to get the result.

if else

If-Else

The ‘If statement’ is a conditional statement that consists of an expression followed by another expression. If the condition is true, the lines under the body of the If statement are executed. If it says the condition to be False, it executes the statements under the Else Part.

if else

if else

For Loop

For loop is a control flow statement that allows the user to write a loop that can be executed repeatedly.

Example:

for loop

The following code will prompt the message box showing the value from 0 to 6 with the gap of 2. 

for 2

While Loop

In a while loop, if the statements are true, they are executed till they encounter the Wend keyword. If the statement is false, the loop is exited, and it jumps to the next statement.

Syntax: 

While condition(s)

   [statements 1]

   [statements 2]

   ...

   [statements n]

Wend

Example:

while 1

while 2

Functions and Sub Procedures

Functions

Functions are the reusable code that can be called anywhere in the program. You can use the code over and over again in your program. To create the function in the VBA window, go to Insert > Module and it will create a new module.

The following code shows an example of a simple VBA function procedure that receives two arguments. 

function

The below-given code calls the function that was defined above: 

function

Result:

function

Sub-Procedure

Sub procedures are similar to functions with some minor differences. The sub-procedure does not return a value and can be called without a call keyword.

The following code shows an example of a simple VBA Sub procedure that will calculate the area. 

sub 1

The above example illustrates how sub procedures perform actions but do not return values.

excel vba

Result:

sub 3

Gain expertise in the latest Business analytics tools and techniques with the Business Analyst Master's Program. Enroll now!

Conclusion

With this, you have made it to the end of this Excel VBA tutorial for beginners. In this tutorial, we have covered several topics ranging from VBA editor to functions and sub-procedures.

Boost your analytics career with powerful new Microsoft Excel skills by taking the Business Analytics with Excel course, which includes Power BI training

This Business Analytics certification course teaches you the basic concepts of data analysis and statistics to help data-driven decision making. This training introduces you to Power BI and delves into the statistical concepts that will help you devise insights from available data to present your findings using executive-level dashboards.

Do you have any questions for us? Feel free to ask them in this article’s comments section, and our experts will promptly answer them for you! 

About the Author

Aryan GuptaAryan Gupta

Aryan is a tech enthusiast who likes to stay updated about trending technologies of today. He is passionate about all things technology, a keen researcher, and writes to inspire. Aside from technology, he is an active football player and a keen enthusiast of the game.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.