0% found this document useful (0 votes)
8 views11 pages

VBA Conditional Structure Guide

The document outlines the steps to write coding in VBA for Microsoft Excel, emphasizing the use of conditional structures such as If-Then-Else statements. It explains how to declare variables, insert inputs, and display outputs, along with syntax examples for conditional structures. Additionally, it includes an exercise to create a program for pricing callable bonds using VBA.

Uploaded by

2024963803
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

VBA Conditional Structure Guide

The document outlines the steps to write coding in VBA for Microsoft Excel, emphasizing the use of conditional structures such as If-Then-Else statements. It explains how to declare variables, insert inputs, and display outputs, along with syntax examples for conditional structures. Additionally, it includes an exercise to create a program for pricing callable bonds using VBA.

Uploaded by

2024963803
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASC454: ACTUARIAL

PROGRAMMING
CONDITIONAL STRUCTURE

Ms Sarah Nadirah Mohd Johari, 2021


STEPS TO WRITE CODING IN VBA MICROSOFT
EXCEL
1. Opening syntax
Sub NameOfYourProgram()
2. Determine and declare the variables/inputs&outputs
Dim NameOfYourVariables/Inputs/Outputs As DataType
3. Insert/Store input
4. Write the process/method syntax
5. Display the output
6. Ending syntax
End Sub

Ms Sarah Nadirah Mohd Johari, 2021


CONDITIONAL STRUCTURE (RECAP)
• Tests a given condition and returns one value for a TRUE result and another value
for a FALSE result.

OR

Ms Sarah Nadirah Mohd Johari, 2021


CONDITONAL STRUCTURE (RECAP)
• Tests a given condition and returns one value for a TRUE result and another
value for a FALSE result.

OR

OR

SMALL OR MEDIUM OR LARGE

Ms Sarah Nadirah Mohd Johari, 2021


CONDITONAL STRUCTURE
• If – Then – Else statement.
• Executes one set of code if a specified condition results TRUE or another
set of code if it results FALSE.
• Built in function that categorized under Logical Function.

Ms Sarah Nadirah Mohd Johari, 2021


CONDITONAL STRUCTURE SYNTAX
If condition_1 Then If condition_1 Then
result_1 result_1
Else Elseif condition_2 Then
result_2 result_2
End If …
Elseif condition_n Then
result_n
Else
result_else
End If

Ms Sarah Nadirah Mohd Johari, 2021


CONDITONAL STRUCTURE SYNTAX
• Condition_1, condition_2, … , condition_n
Evaluated in the order listed. Once a condition is found to be true, the
corresponding code will be executed. No further conditions will be
evaluate.
• Result_1, result_2, … , result_n
Executed once a condition is found to be true
• Result_else
Executed when all previous conditions (Condition_1, condition_2, … ,
condition_n) are false

Ms Sarah Nadirah Mohd Johari, 2021


CONDITONAL STRUCTURE EXAMPLE
FIRST EXAMPLE:
If LRegion = “N" Then
LRegionName = “North”
End If

Ms Sarah Nadirah Mohd Johari, 2021


CONDITONAL STRUCTURE EXAMPLE
SECOND EXAMPLE:

If LRegion = “N" Then


LRegionName = “North”
ElseIf LRegion = “S” Then
LRegionName = “South”
ElseIf LRegion = “E” Then
LRegionName = “East”
ElseIf Lregion = “W” Then
LRegionName = “West”
End If

Ms Sarah Nadirah Mohd Johari, 2021


CONDITONAL STRUCTURE EXAMPLE
THIRD EXAMPLE:

If LRegion = “N" Then


LRegionName = “North”
ElseIf LRegion = “S” Then
LRegionName = “South”
ElseIf LRegion = “E” Then
LRegionName = “East”
Else
LRegionName = “West”
End If

Ms Sarah Nadirah Mohd Johari, 2021


EXERCISE
Create a template and write a program for the Price of Callable Bond and
determine whether the bond is purchase in premium or discount in
Microsoft Excel VBA.

Ms Sarah Nadirah Mohd Johari, 2021

You might also like