Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What is Excel VBA? Explained with Real Time Examples

Updated on 14 November, 2024

7.23K+ views
10 min read

What Is VBA?

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.

How is VBA Used?

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.

  • General Users: Generally, users use Excel and other MS Office programs. The user can utilize the MS Office Suite with VBA language free of cost. VBA is used to create and organize spreadsheets. In addition, it is used in numerous other tasks such as if a user has to automate some Excel features like repetitive tasks, frequent tasks, generating reports, etc then it can be done by using VBA. For this purpose, the user can create a VBA program within Excel to produce format and print monthly sales reports with graphical representations like bar graphs.
  • Computer Professional: Computer professionals can use VBA to perform more complicated tasks that would otherwise require more time and resources. VBA offers some new functions that are not available in Excel such as creating custom add-ins that enhance the application’s functionality, rewriting lengthy lines of code, creating custom languages within Microsoft Office applications, and combining the functions of multiple programs, etc.
  • Corporate Users: The corporate users of various companies use the VBA programming language to automate critical business procedures and internal processes, accounting procedures, minute records, real-time processing of sales orders, complex data calculations, and more.

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.

Why use Excel VBA

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.

  1. By using Macro Recorder: In this technique as soon as the recorder is turned on, Excel will begin to capture every action the user takes and save it as a macro, or “process”. When the user closes the recorder, the macro is saved and can be linked to a button that, when clicked, will repeat the entire process. To use this technique no prior knowledge of VBA is required.
  2. Using VBA: This is a robust method where Excel macros can be programmed using VBA.
     

Also, check out: Job-ready Program in Business Analytics

Where to code Excel VBA

  1. Within any Office program, press Alt F11 to open the VBA window. 
  2. This will open a window with a file structure tree, the coding section that occupies most of the screen in the center.
  3. The coding takes place in the coding section. Here, the user can create macros and save them.
  4. Once the macro code has been created and saved, it can be attached to certain triggers in the Excel model.
  5. The macro can be activated after pressing a certain worksheet button.

VBA shortcuts in Excel

Some shortcuts that operate while using VBA in Microsoft Excel are listed below.

  • Alt F11: To open the VBA editor
  • Alt F8: To view all macros
  • Alt F4: To exit the VBA Editor and return to Excel
  • F1: To display Help
  • Ctrl space: Autocomplete 
  • Alt F5: To run error handler
  • Alt F6: To swap the last two windows
  • Alt F11: To switch between Excel and the VBA editor
  • F7: To start on the code editor
  • F10: To start the menu bar
  • Home: Starting of line

You can apply these shortcuts in a practical background with the help of the Executive PG Program in Business Analytics from LIBA

What can you do with VBA

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.

  1. Write Macros: Macros permit all financial professionals like investment bankers, research analysts, salesmen, traders, Accountants, commercial bankers, clerks, and administrators. To compose macros to quickly analyze and modify massive amounts of data.
  2. Update Data: VBA in Excel is used to forecast sales and earnings, produce financial ratios, and build and maintain trading, pricing, and risk management models.
  3. Conduct a scenario analysis: You can create various scenarios for portfolio management and investment using Visual Basic. 
  4. Organize the information: VBA can also be used to create invoices, forms, and charts, analyze scientific data, and manage data displays for budgeting and forecasting. It can also be used to create lists of customers’ names.
  5. Be unconventional: Values can be copied and pasted, cell styles for an entire workbook can be changed, and accelerator keys can be pressed using VBA. You can execute very common tasks in an easier and more automated manner. 
  6. Prompt Action: You can communicate with users by using VBA. 

Acquire the above mentioned expertise with a deep insight into Professional Certificate Program in Data Science and Business Analytics.

Enable the Developer Option in Excel

On the ribbon, it by default hides the Developer tab. Follow the instructions listed below to customize the ribbon.

  1. First right-click on the ribbon and then click on the Customize the Ribbon option.
  2. Select the Developer checkbox under Customize the Ribbon.

VBA Editor Interface

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.

Create an Excel Macro using a Command Button

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.

Create an Input Box

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.

Variables, Constant, and Operators in VBA

  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. 

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

  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, If-Else, For and While Loop

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

Functions and Sub Procedures

  • Functions: Code that can be reused and called repeatedly throughout a program is called a function. The code can be reused numerous times in your program. Go to Insert and then click on Module in the VBA window to create the function.

This function will call the below code.

The result will be

  • Sub Procedures: With some minor exceptions, sub-procedures and functions are similar. The call keyword is not required to invoke the sub-procedure because it does not return a value.

Example: Program to calculate area using sub procedures.

Conclusion

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. 

Frequently Asked Questions (FAQs)

1. What is the full form of VBA?

The full form of VBA is Visual Basic for Applications.

2. What is the meaning of VBA?

The VBA meaning is simple. It is a powerful programming language developed by Microsoft Corp. that is 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.

3. What is Excel VBA?

Excel VBA is Visual Basic for Applications in the Context of Excel.

4. What is the shortcut key to open the VBA window?

Within any Office program, press Alt F11 to open the VBA window.