Home >> VBA Tutorial
VBA Introduction:
How to Open VBA Editor:
There are two ways by which you can open the VBA Editor window. These are given below.
1. One simplest way is to press Alt+F11. This will open VBA editor where you can write VBA code.
2. Second ways is through menus appears on the top of Excel window. Go to:
Tools
Macro
Visual Basic Editor
When ever you choose one the above option following window will appear on your screen. This is the window where you can write VBA code.
Where to Write Code:
When you will open the Visual Basic Editor you will see different options on the left side of the window. These are Sheet1, Sheet2, Sheet3, and Thisworkbook. Before you write code you need to determine what sort of code you want to write and where it will be best to place. You can write code on particular sheet that will work only for that sheet, or you can write code on Thisworkbook that shared by the all sheets in that workbook. In addition to that you can also use module to write the code. It is best practice to write code on module. You can also use Class Module. This is advance option and not normally used
Declare Variable
Variable are used to store temporary values during the execution of the program. As the name suggest
value can be changed during the execution operation. Dim keyword is used to declare the variables
followed by the name of the variable and Data type of the variable.
Syntax to Declare Variable
Dim name_of_variable as datatype
Dim name_of_variable1 as datatype, name_of_variable2 as Datatype,...........name_of_variableN as datatype
Example
Dim customer_id as integer
Dim customer_Name as string
Dim Order_date as date
Here we define three variable with different data type( integer, string and date). They can be declared in oneline.
Dim customer_id as integer, customer_Name as string, Order_date as date
It is not good programming. Normally we delcare more then one varibale in same line when they have same datatype.
Declare Constant
Constant are used to store fixed values during the execution of the program. As the name suggest
value cannot be changed during the execution operation. Const keyword is used to declare the constants
followed by the name of the constant and Data type of the constant and finaly value that will remain constant during the execution of the program.
Syntax to Declare Constant
Const name_of_constant as datatype = value
Note: We can not Declare more then one constant in Single line.
Example
Const Pie as single=3.1415
Here we define constant pie that is going to store 3.1415 which cannot be changed during the execution.
Declare Static Array
Array can store more then one value of same datatype or we can say array is a collection elements having same datatype.
They hepls to save the time by declaring number of elements with one statement. The size of array determine the number
of elements an array can hold. The first element always stored at index 0 (Elements are accessed by index of array).
Syntax to Declare Static Array
Dim name_of_Array(size of array) as datatype
Dim name_of_Array1(size of array) as datatype, name_of_Array2(size of array) as Datatype,...........name_of_ArrayN(Size of array) as datatype
Example
Dim Number_of_Record(99) as integer
Here we defined Number_of_Record (array name) that can store 100 records, because first record store at 0 index and 100th record will store at 99 index location.
Declare Dynamic Array
Some time there may be situation at daclaring array when you are not sure how much elements you want to store in the array.
In this case static array may create two problem. Either your array size is less then elements, or either it is grater that
mean memory wastage. So handle such situation we can use dynamic array rather then static array.Dim keyword is used to declare dynamic array and "ReDim" keyword is used
to reinitilize the array later in the program where we are sure about how much elements we want to store.
Syntax to Declare Dynamic Array
Dim name_of_Array() as datatype
Dim name_of_Array1() as datatype, name_of_Array2() as Datatype,...........name_of_ArrayN() as datatype
ReDim name_of_Array(known size) as datatype
Example
Dim Number_of_Record() as integer
Here we defined Number_of_Record (array name) that can store many records depending upon users requirement(use ReDim to specify size that user wants).
ReDim Number_of_Record(149) as integer
Here we reinitilize the array and now it can store 150 records.
Comparision Operator
There are six comparision operator. All return boolean value True or False. These are given below.
| Operator | Description |
| = | Returns True if two values are equal else False |
| < | Returns True if Value on left side is less then value on right side else False |
| > | Returns True if Value on Left side is grater then value on the right side else False |
| <= | Returns True if Value on left side is less then or Equal to as compare to value on right side else False |
| >= | Returns True if Value on left side is grater then or Equal to as compare to value on right side else False |
| <> | Returns True if value on left side and value on right side are not equal else return False |