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.
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