Вы находитесь на странице: 1из 13

Basic Components

Basic Components
When 2 or more people communicate, they must know the
language of communication, contents for the debate,
sequence of discussion etc.

Similarly, whenever we want to write code in VBA, we must


know all the basic components and how to declare them

Basic components are as follows:

Variables

Constants

Datatypes

Keywords

Where learning is sharing... 2


Basic Components
(Continue from the previous slide) Basic components :
Operators
Loops
Condition statements
GoTo statement
Cases
Procedures
Functions
Arrays
Most importantly, declaration

Where learning is sharing... 3


Variables, Constants, Keywords
Variable: Variable is a named memory location which stores a
value and this value may change throughout the execution of
the program
Constant: Constant is a named memory location which stores
a value and this value cant change throughout the
execution of the program
Intrinsic Constant: Intrinsic Constants are constants whose
meaning is predefined by VBA compiler like vbBlue, vbRed etc
Keyword: Keywords are reserved word whose meaning is
predefined (already defined) by VBA compiler e.g. For, goto,
vbCrLf, IF, Loop etc

Where learning is sharing... 4


Rules for creating Variables & Constants
Rules for creating Variable and constant names:

Use alphabets (A-Z , a-z) , numbers (0-9) and underscore(_) but 1st
letter must be alphabet

Can not use special characters like +,*,%,$,#

Can not use keywords or reserved words

VBA is non case sensitive language. Hence can not distinguish


between upper and lower case

Where learning is sharing... 5


Datatype
Datatype: Datatype is characteristics of a variable which
determines its attributes such as size, range and type of data it
holds

Default datatype is Variant

Below is the list of datatypes available in VBA:-

Datatype Bytes Used Range


Byte 1 Byte 0 to 255
Boolean 2 Bytes True / False
Integer 2 Bytes -32768 to 32767 (Default = 0)
Long 4 Bytes -2,147,483,648 to 2,147,483,647
Single 4 Bytes -3.402823E38 to -1.401298E-45 (For -VE)
1.401298E-45 to 3.402823E38 (For +VE)

Where learning is sharing... 6


Datatype
Below is the list of datatypes available in VBA:-

Datatype Bytes Used Range


Double 8 Bytes -1.79769313486232E308 to
-4.94065645841247E-324 (For -VE)
4.94065645841247E-324 to
1.79769313486232E308 (For +VE)
Currency 8 Bytes -922,337,203,685,477.5808 to
922,337,203,685,477.5807
Decimal 14 Bytes +79,228,162,514,264,337,593,543,950,335 with no
decimal pt
+7.9228162514264337593543950335 with 28
decimal pt
Date 8 Bytes January 1,0100 to Dec 31,9999
Object 4 Bytes An object reference (Like range, workbook)

Where learning is sharing... 7


Datatype
Below is the list of datatypes available in VBA:-

Datatype Bytes Used Range


String 10 Bytes + 0 to approximately 2 billions
(Variable String
Length) length
String (Fixed Length of 1 to approximately 65,400
Length) string
Variant (With 16 Bytes Any numeric value up to the range of a double
Numbers) datatype
Variant (With 22 Bytes + 0 to approximately 2 billions
String) string
length
User defined Varies Vary by the element

Where learning is sharing... 8


Scope of Variables / Constants
Variables have different scope depend upon their need

Below is the list of scope of variable available in VBA:-


Scope Purpose
Local Within a procedure. When the procedure ends,
the variable no longer exists and Excel frees up
its memory
Modulewide Within a module. i.e. Variable is accessible to all
the procedures in a module. Variable should
declare before 1st procedure of the module
Public Accessible to all the procedures in all the VBA
modules in a project. Use keyword Public
instead of Dim. Variable should declare
before 1st procedure of the module
Static Variable declares at procedure level & they
retain their value when the procedure ends

Where learning is sharing... 9


Declaration of Variables
Now, we know what are variables, what are constants and
what is scope. Lets look at how to declare variable/constants
Syntax:- Dim/public/static variablename [as datatype]
Dim = keyword. Stands for dimension. Used for private,
module wide scope variables
Public = Used only if we want to declare a variable Public
Static = Used only if we want to declare a variable Static
Variablename = any variablename
As = keyword
Datatype = type of data holds

Where learning is sharing... 10


Examples of Variables
Do not use as datatype ; only if you want to declare a
variable with the datatype Variant because Variant is
default datatype
Let us see some examples:
Dim a as integer
Dim b as string
Dim c as double
Dim d
Public e as single
Static n as date

Where learning is sharing... 11


Declaration of Constants

Syntax:- [public] const constantname [as datatype]

Const = keyword. Stands for constant

Public = Used only if we want to declare a constant Public

constantname = any constantname

As = keyword

Datatype = type of data holds

E.g. const w as long

Public const q as string

Where learning is sharing... 12


Operators
Operators: Operators are used for assignment, calculation,
comparison or conditions

Mathematical Operators:

+,-,*,/,^ (exponentiation), & (concatenation), mod, \

Comparison Operators:

=, >, >=, <, <=, <>

Logical Operators:

AND, OR, NOT etc.

Where learning is sharing... 13

Вам также может понравиться