What is Excel VBA? Explained with Real Time Examples
Updated on Nov 14, 2024 | 10 min read | 7.5k views
Share:
For working professionals
For fresh graduates
More
Updated on Nov 14, 2024 | 10 min read | 7.5k views
Share:
Table of Contents
Do you know what VBA stands for? VBA full form is Visual Basic for Applications. This simple, but powerful programming language was developed by Microsoft Corp. And now VBA is broadly used with other Microsoft Office applications, for example, MS Word, MS Excel, MS Access, MS PowerPoint, Publisher, etc. This language is utilized in writing programs for the Windows operating system and runs as an internal programming language in Microsoft Office.
VBA allows techies to customize applications beyond what is usually available with MS Office for example creating user-defined functions, automating computer processes, and accessing Windows APIs. It also allows users to build solutions to improve those applications’ proficiencies. If you are facing problems in effectively importing your contacts from MS Outlook into an Excel spreadsheet, in repeatedly cleaning up fifty tables in Word or you want a specific document to ask the user for input when it opens then VBA will help you perform all these tasks. You do not have to install Visual Basics on your PC rather installing MS Office will help you to achieve the objectives. A user can use VBA in all available latest office versions, from MS Office 97 to MS Office 2013. Among all Excel VBA is the widely used VBA. Visual Basic Excel is used for Applications in the Context of Excel. You can get a deeper understanding of this concept via Master of Science in Business Analytics from GGU.
Check out our free courses related to upskill yourself.
VBA is a programming language that can be used for a variety of tasks, and it is utilized by various types of users for those tasks. The various organizations that utilize VBA include the following.
Learn business analytics courses online from the World’s top Universities. Earn Masters, Executive PG Programmer Certification, or Global Master Certification to fast-track your career.
Although VBA cannot be used to directly alter the main Excel software, the users can learn to create macros to optimize their time in Excel. Excel macros are created in two ways.
Also, check out: Job-ready Program in Business Analytics
Some shortcuts that operate while using VBA in Microsoft Excel are listed below.
You can apply these shortcuts in a practical background with the help of the Executive PG Program in Business Analytics from LIBA.
Finance is fundamentally about handling vast amounts of data, so VBA is omnipresent in the financial services industry. If you work in finance, VBA is probably running within programs you use on a daily basis. By using VBA, you can accomplish the following things.
Acquire the above mentioned expertise with a deep insight into Professional Certificate Program in Data Science and Business Analytics.
On the ribbon, it by default hides the Developer tab. Follow the instructions listed below to customize the ribbon.
To open the VBA Interface press the shortcut key Alt F11. You can also open it by clicking the Visual Basic on the Developer tab.
To create an Excel Macro using a command button follow the below steps.
1. Go to the Developer tab, then click on Insert, then ActiveX controls and then select the Command button.
Developer tab > Insert > ActiveX Controls > Command button
2. Select and drag the command button on your worksheet.
3. Right-click on the command buttons and select the View Code option.
4. Add the VBA code excel that is given below.
Private Sub CommandButton1_Click()
Range(“A1”).Value= “Learnbasics”
End sub
5. Close the VBA editor and click on the command button on the worksheet.
The following steps are to be followed to create an input box in Excel that will prompt a message on your worksheet.
1. The Syntax used to create an input box is:
InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile,context])
2. To add the InputBox to the command button first declare the variable name and keep the type variant to hold any type of value.
3. Write the following code to show the input box.
Private Sub CommandButton1_Click()
Dim Learnbasics As Variant
Learnbasics = InputBox(“Enter your message”)
Range(“A1”).Value= “Learnbasics”
End sub
4. Now click on the command button to get a prompt asking you to enter your message.
5. Enter your message and click OK. It will be entered and shown as input on cell A1.
1. Variable:The data types that are used to store values are known as variables. During program execution, we can modify the variables.
Syntax: Dim <<Name_of_variable >> As <<Type_of_variable>>
VBA data types can be classified into two categories.
2. Constants:The fixed value known as a constant is one that cannot be changed while a program is running.
Syntax:
Const <<constant_name>> As <<constant_type>> = <<constant_value>>
Example:
Private Sub CommandButton1_Click()
Const MyInteger As Integer = 10
Range(“A1”).Value= MyInteger
Const MyDay As String = Sunday
Range(“C1”).Value= MyDay
End sub
If Statement
The ‘If statement’ is a conditional statement. It consists of an if statement followed by another statement. If the condition is true, the codes under the If statement are executed.
Syntax for If statement:
If(boolean_expression) Then
Statement 1
…..
…..
Statement n
End If
Example: Program to show the implementation of if statement.
Private Sub CommandButton1_Click()
Dim score As Integer, result As String
Score = Range(“A1”).Value
If score >= 10 Then result = “First”
Range(“B1”).Value = result
End sub
If Else Statement
The ‘If statement’ is a conditional statement. This statement consists of an “If” expression followed by another “Else” expression. If the condition is true, the lines under the body of the If statement are executed and if the condition is false, it executes the line under the Else Part.
Example: Program to show the implementation of if statement.
Private Sub CommandButton1_Click()
Dim A As Integer
Dim B As Integer
A = 1500
B = 1000
If A > B Then
MsgBox “A is greater”
Else
MsgBox “B is greater”
End If
End sub
For Loop:
For loop is a control flow statement. In this, the user can write a loop that can be executed repeatedly till it satisfies the condition.
Example: Program to show the implementation of for loop.
Private Sub CommandButton1_Click()
Dim A As Integer
A = 1500
For i = 0 To Step 2
MsgBox “The value is: ” & i
Next
End sub:
While Loop:
When statements are true in a while loop, they are carried out until the Wend keyword is reached. The loop is ended and the subsequent statement is executed if the statement is false.
Syntax:
While condition(s)
[statements 1]
[statements 2]
…
[statements n]
Wend
This function will call the below code.
The result will be
Example: Program to calculate area using sub procedures.
The basic concept of VBA is described here. This simple, but powerful programming language was developed by Microsoft Corp. The use of Excel VBA, and different types of keywords are illustrated here. Various examples are represented to give an idea about the implementation of an if statement, if else statement, while loop, for loop, etc. Try pursuing Job-ready Program in Business Analytics which will give you an edge over others in the competitive world.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources