SlideShare a Scribd company logo
1 of 46
Excel VBA
Quadra Plus
What is VBA
• 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
• Enable Developer Option in Excel
Right-click on the ribbon (anywhere) click on the Customize the
Ribbon option.
• Go to Customize the Ribbon and select the Developer checkbox.
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.
Create an Excel Macro using 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.
Drag the command button on your worksheet
• Assigning a Macro to a Command Button
• Add the following lines of code shown below.
Close the VBA editor and click on the command button on the
worksheet. Make sure to deselect the design mode.
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.
Now, when you click on the button, you will get the following message.
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.
• 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.
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:
• When you click on the command button, you will get the following
output:
• 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.
Click on the command button to get the following result.
• Left
• The left keyword will extract the characters from the leftmost side of
the string.
• 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:
• Enter the marks in cell A1 and click on the command button to get
the result.
• 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.
• For Loop
• For loop is a control flow statement that allows the user to write a
loop that can be executed repeatedly.
• Example:
• The following code will prompt the message box showing the value
from 0 to 6 with the gap of 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:
• 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.
• The below-given code calls the function that was defined above:
• 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.
• The above example illustrates how sub procedures perform.
• Result:
Thankyou

More Related Content

Similar to Excel VBA.pptx

Visual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdfVisual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdfsheenmarie0212
 
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision table
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision tableProgramming aids- Algorithm, Flowchart, Pseudocodes and Decision table
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision tableAnjali Technosoft
 
Unit ii introduction to vba
Unit ii introduction to vbaUnit ii introduction to vba
Unit ii introduction to vbaDhana malar
 
Python-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptxPython-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptxmuzammildev46gmailco
 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxAnasYunusa
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdfssusere19c741
 
Hsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfHsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfAAFREEN SHAIKH
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosNick Weisenberger
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxssusere336f4
 
Vba and macro creation (using excel)
Vba and macro creation (using excel)Vba and macro creation (using excel)
Vba and macro creation (using excel)Javier Morales Cauna
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptxStefan Oprea
 

Similar to Excel VBA.pptx (20)

Visual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdfVisual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdf
 
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision table
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision tableProgramming aids- Algorithm, Flowchart, Pseudocodes and Decision table
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision table
 
Module-1.pptx
Module-1.pptxModule-1.pptx
Module-1.pptx
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Unit ii introduction to vba
Unit ii introduction to vbaUnit ii introduction to vba
Unit ii introduction to vba
 
Learn Excel Macro
Learn Excel Macro  Learn Excel Macro
Learn Excel Macro
 
Ma3696 Lecture 1
Ma3696 Lecture 1Ma3696 Lecture 1
Ma3696 Lecture 1
 
Python-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptxPython-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptx
 
Excel300
Excel300Excel300
Excel300
 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptx
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf
 
VISUAL
VISUALVISUAL
VISUAL
 
Hsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfHsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdf
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel Macros
 
Tugas testing
Tugas testingTugas testing
Tugas testing
 
Vbabook ed2
Vbabook ed2Vbabook ed2
Vbabook ed2
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
 
Vba and macro creation (using excel)
Vba and macro creation (using excel)Vba and macro creation (using excel)
Vba and macro creation (using excel)
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptx
 

More from GiyaShefin

ccnp routing.pptx
ccnp routing.pptxccnp routing.pptx
ccnp routing.pptxGiyaShefin
 
ccna3mod9_VLAN Trunking Protocol (1).pptx
ccna3mod9_VLAN Trunking Protocol (1).pptxccna3mod9_VLAN Trunking Protocol (1).pptx
ccna3mod9_VLAN Trunking Protocol (1).pptxGiyaShefin
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.pptGiyaShefin
 
Core python.pptx
Core python.pptxCore python.pptx
Core python.pptxGiyaShefin
 
Python ppt.potx
Python ppt.potxPython ppt.potx
Python ppt.potxGiyaShefin
 

More from GiyaShefin (6)

ccnp routing.pptx
ccnp routing.pptxccnp routing.pptx
ccnp routing.pptx
 
ccna3mod9_VLAN Trunking Protocol (1).pptx
ccna3mod9_VLAN Trunking Protocol (1).pptxccna3mod9_VLAN Trunking Protocol (1).pptx
ccna3mod9_VLAN Trunking Protocol (1).pptx
 
10_introduction_php.ppt
10_introduction_php.ppt10_introduction_php.ppt
10_introduction_php.ppt
 
07-PHP.pptx
07-PHP.pptx07-PHP.pptx
07-PHP.pptx
 
Core python.pptx
Core python.pptxCore python.pptx
Core python.pptx
 
Python ppt.potx
Python ppt.potxPython ppt.potx
Python ppt.potx
 

Recently uploaded

Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023ymrp368
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxMohammedJunaid861692
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Onlineanilsa9823
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxolyaivanovalion
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlCall Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlkumarajju5765
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 

Recently uploaded (20)

Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptx
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlCall Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 

Excel VBA.pptx

  • 2. What is VBA • 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 • Enable Developer Option in Excel Right-click on the ribbon (anywhere) click on the Customize the Ribbon option.
  • 3. • Go to Customize the Ribbon and select the Developer checkbox.
  • 4. 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.
  • 5. Create an Excel Macro using 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.
  • 6.
  • 7. Drag the command button on your worksheet
  • 8. • Assigning a Macro to a Command Button
  • 9. • Add the following lines of code shown below.
  • 10. Close the VBA editor and click on the command button on the worksheet. Make sure to deselect the design mode.
  • 11. 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.
  • 12.
  • 13. Now, when you click on the button, you will get the following message.
  • 14. 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.
  • 15.
  • 16. • Now when you click on the command button, you will get a prompt asking for a value
  • 17. • 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.
  • 18.
  • 19. 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:
  • 20.
  • 21. • When you click on the command button, you will get the following output:
  • 22. • 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.
  • 23.
  • 24. Click on the command button to get the following result.
  • 25. • Left • The left keyword will extract the characters from the leftmost side of the string.
  • 26.
  • 27. • 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:
  • 28.
  • 29. • Enter the marks in cell A1 and click on the command button to get the result.
  • 30. • 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.
  • 31.
  • 32.
  • 33. • For Loop • For loop is a control flow statement that allows the user to write a loop that can be executed repeatedly. • Example:
  • 34. • The following code will prompt the message box showing the value from 0 to 6 with the gap of 2.
  • 35. • 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:
  • 36.
  • 37.
  • 38. • 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.
  • 39.
  • 40. • The below-given code calls the function that was defined above:
  • 41.
  • 42. • 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.
  • 43.
  • 44. • The above example illustrates how sub procedures perform.