Visual Basic For Application
Visual Basic For Application
Dr. A. Okullo 2
Macro procedures or sub procedures are
often recorded and are easily automated.
Function procedures or custom functions or
user defined functions are normally
developed by the user. Custom functions can
be used on Excel worksheet just like any
other Excels in built function. The structures
of these two VBA procedures are given below:
Dr. A. Okullo 3
Structure of a sub procedure:
Sub procedure Name (Argument, Argument,
)
VBA statements
VBA Statements ..
End Sub
Structure of a Function procedure:
Function FunctionName (Argument, Argument,)
VBA statements
VBA Statements .
FunctionName = Results
End Function
Dr. A. Okullo 4
As you type the code, VB editor will be
checking syntax errors. If there are errors
the syntax is indicated by red. Variables are
displayed in black, comments are green and
some VBA key words are blue (e.g. function,
range, etc).
At the top of each module a declaration
may be typed: Option Explicit
This forces the user to declare all variables
starting with Dim statements, followed by
data type.
Dr. A. Okullo 5
It is good programming practice to use Tab
when typing a module to indent related
lines for easier reading. For example;
Sub Initialize ()
For J = 1 To N
P(J) = 0
Next J
End Sub
To produce a more compact procedure, several line
codes can be combined in one line, separated by
a colon(:) e.g
Sub Initialize ()
For J = 1 To N: P(J) = 0: Next J
End Sub
Dr. A. Okullo 6
A simple example of a Function procedure
could be for converting temperature in oF to
oC, lets call it FtoC and it goes like this:
Dr. A. Okullo 7
DegF is the argument (variable) which is
passed by the Function from the
worksheet to the module (programme),
they are values of oF temp on the
worksheet.
The FtoC line (function name) calculates the Celsius
temperature and returns the results to the caller (the
cell in the worksheet in which the function is
entered).
NB: function names should be short since you will be
typing them in the worksheet but long enough to
convey information as to what it does. You can now
use your function just like you would use any inbuilt
excel function. The workbook containing your
function must be open.
Dr. A. Okullo 8
For the example of the temperature
conversion, open an excel workbook, create
the module as indicated then return to a
worksheet and try it with a few numbers as
indicated in the table below:
Dr. A. Okullo 9
A2 has 212,
the argument
that your
custom
function will A B Type the
use 1 T(oF) T(oC) formula in this
2 212 cell
3 200 =FtoC(A2) and
4 180 copy it down.
5 150 See whether
6 100 the results are
7 80 logical.
8 50
9 32
10 25
11 0
Dr. A. Okullo 10
Dr. A. Okullo 11
Branching
VBA supports If . Then statements. The
syntax of If . Then is
If LogicalExpression Then statement1 Else
statement2. The If Then statement can
be a simple one, for example,
If (x>0) Then number = 10^x
If logicalexpression is True (in this case
x>0), the statement is carried out. If
logicalexpression is False, nothing is done
(the programme moves to the next line)
Dr. A. Okullo 12
If ..Then...Else structures are also
possible. E.g. If Err.Number = 13 Then
Resume pt1 Else End
In a block If statement, If
logicalexpression Then is followed by
numerous statement lines and is
terminated by End if for example
Dr. A. Okullo 13
If . Then.ElseIf structures are also
possible. For example;
Dr. A. Okullo 14
Logical Operators
Logical operators And, Or and Not can be
used in LogicalExpression, for example
If C >= 0 And C<= 9 Then
Select Case
VBA also provides the Select Case decision
structure similar to the ON value GOTO
statement in BASIC. The Select Case
statement is an efficient alternative to the
series of ElseIf conditionN statements when
conditionN is a single expression that can
take various values.
Dr. A. Okullo 15
The syntax is:
Dr. A. Okullo 16
Statement is executed if TestExpression does not
match any of the values in any ExpressionN
Looping
Looping structure in VBA is similar to those
available in other programming languages.
For .. Next . Loop
The syntax is
For Counter = start To End Step Increment
Statements
Next Counter
Dr. A. Okullo 17
For Example:
For J = 1 To 100
Statement s
Next J
The Step increment statement is
optional. If increment is omitted, it is set
to equal 1. Increment can be negative or
non-integral; for example: For J = 100 to
0 step -1
Dr. A. Okullo 18
The do loop is used when you dont know
before hand how many times the loop will be
executed. You can loop while the condition is
True or until the condition becomes True. As
shown below:
Dr. A. Okullo 19
This second one executes the loop at
least once.
Nested Loop
Often one loop is nested inside another
as shown below;
Dr. A. Okullo 20
You often use a loop structure to search
through an array or collection of objects,
looking for a certain value or property.
Once a match is found you dont need to
cycle through the rest of the loop. You can
exit from the loop using the Exit For
from a For Next loop or Exit Do from a
Do While loop. The Exit statement is
normally located within the If statement, for
example;
If CellContents.Value <=0 Then Exit For
Use Exit Sub or Exit Function to exit from a
procedure. Exit statement can appear as
many times as needed within a procedure.
Dr. A. Okullo 21