SlideShare a Scribd company logo
1 of 17
Download to read offline
VISUAL BASIC FOR
APPLICATIONS WITH
EXCEL
 Variables and data types



Constants



Simple input and output



String functions


Declaring Variables :



To declare a variable, use a Dim (short for Dimension) statement.



Dim myVar As Integer



The name of the variable is myVar.
Note :- The name must begin with an alphabetic character and cannot exceed 255

characters or contain any spaces.



Use Option Explicit in the general declarations section of a module
window to force explicit variable declarations
Object and Standard Modules








Modules refer to a related set of declarations and procedures.
Each module will have a separate window in the VBA IDE and, depending on the
origination of the module, it will have different behavior with regard to variable
declarations.
This module will automatically contain all event procedures associated with the
worksheet Sheet1, and any ActiveX controls added to this worksheet.
A standard module must be added to the project via the Insert menu of the VBA IDE.
The object
module
for an Excel
worksheet.
Inserting a
standard module.
 Variable

Scope

Scope, in the context of variables, refers to the time when a
variable is visible or available to the program. When a variable
is in its scope, it can be accessed and/or anipulated. When a
variable is out of scope, it is unavailable—essentially invisible
to the program.
Private Sub Worksheet Activate()

Static myVar4 As Integer
myVar4 = myVar4 + 1
End Sub

In this procedure the variable myVar4 will increment its value by one with each call to the
procedure. If you replace the Static keyword with Dim, myVar4 will never exceed a value
of 1. Integer variables are initialized to a value of 0 at declaration.
Data Types




Data types define the kind of value that may be stored within the memory
allocated for a variable.
As with spreadsheet cells, there are numerous data types
Data Type

Storage Size

Range

Boolean

2 bytes

True or False

Integer

2 bytes

-32,768 to 32,767

Long

4 bytes

- 2,147,483,648 to
2,147,483,647

Single (floating-point)

4 bytes

- 3.402823E38 to
1.401298E-45 for negative
values; 1.401298E-45 to
3.402823E38 for positive
values

Currency

8-byte

922,337,203,685,477.580
8 to
922,337,203,685,477.580
7

Object

4 bytes

Any Object reference

String (variable-length

10 bytes + string length

0 to approximately 2
billion






Integer, long, single, and double.
A variable declared as an integer or long data type can hold whole numbers or nonfractional values within the specified ranges.
Common Mathematical Operators Used In VBA

Operation

Operator

Addition

+

Subtraction

-

Multiplication

*

Division

/

Exponential

^


Basically, any mathematical operation that can be performed on a number
can be performed on a numerical variable. The following are a few
examples:
Dim num1 As Integer
Dim num2 As Integer
Dim answer As Integer
num1 = 10
num2 = 5
answer = num1 + num2 ' answer Holds 15
answer = num1 num2 ' answer Holds 5
answer = num1 * num2 ' answer Holds 50
answer = num1 / num2 ' answer Holds 2
answer = num1 ^ 2 ' answer Holds 100
answer = 2 ^ num2 ' answer Holds 32






Variables with string data types are used to hold characters as
text. The characters can be numbers, letters, or special symbols
(for example, punctuation marks).
Basically, just about anything you can type on your keyboard can
be held within a string variable. To declare a variable with the
string data type, use the String keyword. To initialize a string
variable, place the string value within double quotation marks.
Dim myText As String
myText = “VBA is fun”

Dim myString As String * 8
myString = "ABCDEFGHIJKL“




Variant data types are analogous to the General category in the number format of a
spreadsheet cell in the Excel application.

Variables are declared as variants by using the keyword Variant, or by not specifying a
data type.



Dim myVar
Dim myVar2 As Variant



Variant type variables can hold any type of data except a fixed length string







Dim myVar As Integer
myVar = 10
myVar = "Testing"
The example above will generate a type mismatch error because an attempt is made to enter

the string "Testing" into an integer variable; however, if you change the variable myVar to a
variant, the code will execute and myVar will hold the string value "Testing" when all is complete.


The following code will run without error.



Dim myVar



myVar = 10



myVar = "Testing“

Boolean Data Types:
The Boolean data type holds the value true or false.

Declare and initialize a Boolean variable as follows:


Dim rollDice As Boolean



rollDice = False



Date Data Types



Variables of type date are actually stored as floating-point numbers with the integer portion
representing a date between 1 January 100 and 31 December 9999, and the decimal portion



representing a time between 0:00:00 and 23:59:59. The date data type is mostly a convenience



when you need to work with dates or times. Declare and initialize a Date variable as follows:



Dim currentDate As Date



currentDate = Now



A handful of VBA functions use variables of type date that add to this convenience. You will



see a couple of examples of date functions in the chapter project.




Constants allow you to assign a meaningful name to a number or string that will
make your code easier to read
Const PI = 3.14159





Dim circumference As Single
Dim diameter As Single
diameter =10.32
circumference = PI* diameter
Function Name

Returns

Str()

A string representation of a number

Val()

A numerical representation of a string

Trim()

A string with leading and trailing spaces
removed

Left()

A portion of a string beginning from the left
side

Right()

A portion of a string beginning from the
right side

Mid()

Any portion of a string

More Related Content

What's hot

Unit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programmingUnit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programming
Abha Damani
 
Basic controls of Visual Basic 6.0
Basic controls of Visual Basic 6.0Basic controls of Visual Basic 6.0
Basic controls of Visual Basic 6.0
Salim M
 

What's hot (19)

Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
Financial modeling sameh aljabli lecture 6
Financial modeling   sameh aljabli   lecture 6Financial modeling   sameh aljabli   lecture 6
Financial modeling sameh aljabli lecture 6
 
User define data type In Visual Basic
User define data type In Visual Basic User define data type In Visual Basic
User define data type In Visual Basic
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2
 
Excel-VBA
Excel-VBAExcel-VBA
Excel-VBA
 
Vba part 1
Vba part 1Vba part 1
Vba part 1
 
2016 Excel/VBA Notes
2016 Excel/VBA Notes2016 Excel/VBA Notes
2016 Excel/VBA Notes
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSVISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
 
Objects and classes in Visual Basic
Objects and classes in Visual BasicObjects and classes in Visual Basic
Objects and classes in Visual Basic
 
Vba and macro creation (using excel)
Vba and macro creation (using excel)Vba and macro creation (using excel)
Vba and macro creation (using excel)
 
Unit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programmingUnit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programming
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programming
 
Part 12 built in function vb.net
Part 12 built in function vb.netPart 12 built in function vb.net
Part 12 built in function vb.net
 
Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
 
Excell vba
Excell vbaExcell vba
Excell vba
 
Excell vba
Excell vbaExcell vba
Excell vba
 
Basic controls of Visual Basic 6.0
Basic controls of Visual Basic 6.0Basic controls of Visual Basic 6.0
Basic controls of Visual Basic 6.0
 

Viewers also liked (9)

Excel y visual basic
Excel y visual basicExcel y visual basic
Excel y visual basic
 
Notes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsNotes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculations
 
Programming inexcelvba anintroduction
Programming inexcelvba anintroductionProgramming inexcelvba anintroduction
Programming inexcelvba anintroduction
 
Intro macros in Excel 2007
Intro macros in Excel 2007Intro macros in Excel 2007
Intro macros in Excel 2007
 
Belajar macro excel 2007
Belajar macro excel 2007Belajar macro excel 2007
Belajar macro excel 2007
 
Excel ch10
Excel ch10Excel ch10
Excel ch10
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programming
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computer
 
Introduction To Excel 2007 Macros
Introduction To Excel 2007 MacrosIntroduction To Excel 2007 Macros
Introduction To Excel 2007 Macros
 

Similar to Visual Basics for Application

Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
J. C.
 

Similar to Visual Basics for Application (20)

E learning excel vba programming lesson 3
E learning excel vba programming  lesson 3E learning excel vba programming  lesson 3
E learning excel vba programming lesson 3
 
Variables and calculations_chpt_4
Variables and calculations_chpt_4Variables and calculations_chpt_4
Variables and calculations_chpt_4
 
Qtp - Introduction to fundamentals of vbscript
Qtp - Introduction to fundamentals of vbscriptQtp - Introduction to fundamentals of vbscript
Qtp - Introduction to fundamentals of vbscript
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Variable and constants in Vb.NET
Variable and constants in Vb.NETVariable and constants in Vb.NET
Variable and constants in Vb.NET
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
Keywords, identifiers and data type of vb.net
Keywords, identifiers and data type of vb.netKeywords, identifiers and data type of vb.net
Keywords, identifiers and data type of vb.net
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
Visual basic intoduction
Visual basic intoductionVisual basic intoduction
Visual basic intoduction
 
Vbscript
VbscriptVbscript
Vbscript
 
UNIT-II VISUAL BASIC.NET | BCA
UNIT-II VISUAL BASIC.NET | BCAUNIT-II VISUAL BASIC.NET | BCA
UNIT-II VISUAL BASIC.NET | BCA
 
VB Script Overview
VB Script OverviewVB Script Overview
VB Script Overview
 
JAVA LESSON-01.pptx
JAVA LESSON-01.pptxJAVA LESSON-01.pptx
JAVA LESSON-01.pptx
 
Learning VB.NET Programming Concepts
Learning VB.NET Programming ConceptsLearning VB.NET Programming Concepts
Learning VB.NET Programming Concepts
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Visual Basic Fundamentals
Visual Basic FundamentalsVisual Basic Fundamentals
Visual Basic Fundamentals
 
Introduction to Visual Basic
Introduction to Visual Basic Introduction to Visual Basic
Introduction to Visual Basic
 
Pj01 3-java-variable and data types
Pj01 3-java-variable and data typesPj01 3-java-variable and data types
Pj01 3-java-variable and data types
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
datatypes_variables_constants
datatypes_variables_constantsdatatypes_variables_constants
datatypes_variables_constants
 

More from Raghu nath

Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)
Raghu nath
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
Raghu nath
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raghu nath
 
Selection sort
Selection sortSelection sort
Selection sort
Raghu nath
 
Binary search
Binary search Binary search
Binary search
Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
Raghu nath
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithms
Raghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp role
Raghu nath
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4
Raghu nath
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
Raghu nath
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1
Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
Raghu nath
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 

More from Raghu nath (20)

Mongo db
Mongo dbMongo db
Mongo db
 
Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)
 
MS WORD 2013
MS WORD 2013MS WORD 2013
MS WORD 2013
 
Msword
MswordMsword
Msword
 
Ms word
Ms wordMs word
Ms word
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Selection sort
Selection sortSelection sort
Selection sort
 
Binary search
Binary search Binary search
Binary search
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithms
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp role
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Perl
PerlPerl
Perl
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Visual Basics for Application

  • 2.  Variables and data types  Constants  Simple input and output  String functions
  • 3.  Declaring Variables :  To declare a variable, use a Dim (short for Dimension) statement.  Dim myVar As Integer  The name of the variable is myVar. Note :- The name must begin with an alphabetic character and cannot exceed 255 characters or contain any spaces.  Use Option Explicit in the general declarations section of a module window to force explicit variable declarations
  • 4. Object and Standard Modules     Modules refer to a related set of declarations and procedures. Each module will have a separate window in the VBA IDE and, depending on the origination of the module, it will have different behavior with regard to variable declarations. This module will automatically contain all event procedures associated with the worksheet Sheet1, and any ActiveX controls added to this worksheet. A standard module must be added to the project via the Insert menu of the VBA IDE.
  • 5. The object module for an Excel worksheet.
  • 7.  Variable Scope Scope, in the context of variables, refers to the time when a variable is visible or available to the program. When a variable is in its scope, it can be accessed and/or anipulated. When a variable is out of scope, it is unavailable—essentially invisible to the program. Private Sub Worksheet Activate() Static myVar4 As Integer myVar4 = myVar4 + 1 End Sub In this procedure the variable myVar4 will increment its value by one with each call to the procedure. If you replace the Static keyword with Dim, myVar4 will never exceed a value of 1. Integer variables are initialized to a value of 0 at declaration.
  • 8. Data Types   Data types define the kind of value that may be stored within the memory allocated for a variable. As with spreadsheet cells, there are numerous data types
  • 9. Data Type Storage Size Range Boolean 2 bytes True or False Integer 2 bytes -32,768 to 32,767 Long 4 bytes - 2,147,483,648 to 2,147,483,647 Single (floating-point) 4 bytes - 3.402823E38 to 1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values Currency 8-byte 922,337,203,685,477.580 8 to 922,337,203,685,477.580 7 Object 4 bytes Any Object reference String (variable-length 10 bytes + string length 0 to approximately 2 billion
  • 10.    Integer, long, single, and double. A variable declared as an integer or long data type can hold whole numbers or nonfractional values within the specified ranges. Common Mathematical Operators Used In VBA Operation Operator Addition + Subtraction - Multiplication * Division / Exponential ^
  • 11.  Basically, any mathematical operation that can be performed on a number can be performed on a numerical variable. The following are a few examples: Dim num1 As Integer Dim num2 As Integer Dim answer As Integer num1 = 10 num2 = 5 answer = num1 + num2 ' answer Holds 15 answer = num1 num2 ' answer Holds 5 answer = num1 * num2 ' answer Holds 50 answer = num1 / num2 ' answer Holds 2 answer = num1 ^ 2 ' answer Holds 100 answer = 2 ^ num2 ' answer Holds 32
  • 12.    Variables with string data types are used to hold characters as text. The characters can be numbers, letters, or special symbols (for example, punctuation marks). Basically, just about anything you can type on your keyboard can be held within a string variable. To declare a variable with the string data type, use the String keyword. To initialize a string variable, place the string value within double quotation marks. Dim myText As String myText = “VBA is fun” Dim myString As String * 8 myString = "ABCDEFGHIJKL“
  • 13.   Variant data types are analogous to the General category in the number format of a spreadsheet cell in the Excel application. Variables are declared as variants by using the keyword Variant, or by not specifying a data type.  Dim myVar Dim myVar2 As Variant  Variant type variables can hold any type of data except a fixed length string     Dim myVar As Integer myVar = 10 myVar = "Testing"
  • 14. The example above will generate a type mismatch error because an attempt is made to enter the string "Testing" into an integer variable; however, if you change the variable myVar to a variant, the code will execute and myVar will hold the string value "Testing" when all is complete.  The following code will run without error.  Dim myVar  myVar = 10  myVar = "Testing“ Boolean Data Types: The Boolean data type holds the value true or false. Declare and initialize a Boolean variable as follows:
  • 15.  Dim rollDice As Boolean  rollDice = False  Date Data Types  Variables of type date are actually stored as floating-point numbers with the integer portion representing a date between 1 January 100 and 31 December 9999, and the decimal portion  representing a time between 0:00:00 and 23:59:59. The date data type is mostly a convenience  when you need to work with dates or times. Declare and initialize a Date variable as follows:  Dim currentDate As Date  currentDate = Now  A handful of VBA functions use variables of type date that add to this convenience. You will  see a couple of examples of date functions in the chapter project.
  • 16.   Constants allow you to assign a meaningful name to a number or string that will make your code easier to read Const PI = 3.14159     Dim circumference As Single Dim diameter As Single diameter =10.32 circumference = PI* diameter
  • 17. Function Name Returns Str() A string representation of a number Val() A numerical representation of a string Trim() A string with leading and trailing spaces removed Left() A portion of a string beginning from the left side Right() A portion of a string beginning from the right side Mid() Any portion of a string