Visual Basic For Applications Training
Visual Basic For Applications Training
2
What is VBA
VBA and why it is useful
VBA stands for Visual Basic for Applications an event driven programming
language from Microsoft that is now predominantly used with Microsoft office
applications such as MS-Excel, MS-Word etc.
Among VBA, Excel VBA is the most popular. The advantage of using VBA is
that you can build very powerful tools in MS Excel using linear programming.
MS-Excel provides only basic inbuilt functions which might not be sufficient to
perform complex calculations. Under such circumstances, VBA becomes the
most obvious solution.
3
Accessing VBA
4
Setting Up the VBA Window
Project Tab
Properties Tab
Background and Text Color
Short Cuts
5
VBA Terminologies
Some Basic Terminologies
Objects : In Excel VBA, an object can contain another object, and that object can
contain another object, etc. In other words, Excel VBA programming involves working
with an object hierarchy.
6
Range Object
In Excel, Excel Workbook itself is the mother of all objects. The hierarchy in
excel follows the following sequence:
Workbook Worksheet Range/Cell/Array etc.Property
7
Your First Program
Hello World
Message Box
8
Adding Comments in VBA
To document user information within the code.
2 methods:
9
VBA - Variables
10
Data Types
Used for Declaration – Numeric Types
11
Data Types
Used for Declaration – Non-Numeric Type
12
VBA - Constants
Constants are declared the same way the variables are declared.
Syntax:
Const <<constant_name>> As <<constant_type>> = <<constant_value>>
13
VBA - Operators
An Operator can be defined using a simple expression - 4 + 5 is equal to 9.
Here, 4 and 5 are called operands and + is called operator.
14
VBA - Operators
The Arithmatic Operators
15
VBA - Operators
The Comparison Operators
16
VBA - Operators
The Logical Operators
17
VBA - Operators
The Concatenation Operators
18
VBA – InputBox and MsgBox Function
InputBox asks the user to enter values.
MsgBox displays a message box and waits for user to click a button.
InputBox and MsgBox are functions that returns a value.
19
Workshop 1
20
Cells Object
Cells (row number, column number)
Can be used with Range Object as
Range(cells(1,2) , cells (3,4))
Useful when using Column Number instead of Column Alphabet name.
21
Decisions
If based statements
22
Workshop 2
Testing If Statements
In the below table we have a Grade Table. Our task is to write a program that
accepts Marks from user and displays the corresponding Grade.
23
Loops
For and Do based statements
24
Loops – Do... While... Loop
Code placed between Do While and Loop will be repeated as long as the part
after Do While is true.
In For Loop, we provide a range to be checked. But a Do-While loop is
conditional.
Syntax :
Dim i As Integer
i=1
Do While i < 6
Cells(i, 1).Value = 20
i=i+1
Loop
As long as i is lower than 6, Excel VBA enters the value 20 into the cell at the intersection
of row i and column 1 and increments i by 1.
25
Loops – Loop Control Statements
Loop Control statements are
used to exit loops.
26
Functions in VBA
A function is a group of reusable code which can be called anywhere in your
program.
The difference between a function and a sub in Excel VBA is that a function
can return a value while a sub cannot. Functions become very useful as
program size increases.
Calling a function in Excel using = Functionname(Input)
27
Special Activity 1 – Interpolation and Extrapolation
Useful if you find yourself needing to interpolate from tables of data
frequently.
Mathematics behind it :
Required items:
An array of known x-values
An array of known y-values
The value of x for which we want to estimate a corresponding y-value
Desired Solution :
A Function which takes ‘Required Items’ as Input and provides interpolated value
as output.
28
VBA - ARRAYS
Array Declaration :
'Method 1 : Using Dim
Dim arr1()'Without Size
29
VBA – ARRAYS... Continued...
Although, the array size is indicated as 5, it can hold 6 values as array index
starts from ZERO.
Array Index cannot be negative.
VBScript Arrays can store any type of variable in an array. Hence, an array
can store an integer, string, or characters in a single array variable.
30
Multi-Dimensional Arrays
Oh yes... Multi-dimensional
Arrays are not just limited to a single dimension, however, they can have a
maximum of 60 dimensions.
Two-dimensional arrays are the most commonly used ones.
31
Re-Dimensioning the Array
Existing Array can be redimensioned.
ReDim statement is used to declare dynamic-array variables and allocate or
reallocate storage space.
32
Array inbuilt functions
33
VBA - Application Object
The mother of all objects is Excel itself. We call it the Application object.
The application object gives access to a lot of Excel related options.
34
VBA - Application Object
ScreenUpdating
Sometimes you may find it useful to disable screen updating (to avoid flickering)
while executing code. As a result, your code will run faster.
35
More...
User Forms custom-built dialog box that makes a user data entry
more controllable and easier to use for the user.
36
Questions…?
shi-fw.com