Professional Documents
Culture Documents
Excel VBA Lecture 2
Excel VBA Lecture 2
Introduction
Making Decisions
Conditional Statements
Syntax (single-line):
statements -> one or more statements (block of code) get executed if the
condition evaluates to True. If statements are not specified, then no code will
be executed if the condition evaluates to True.
ElseIf -> this clause can be used (optionally) if you want to test for multiple
conditions. It is necessary to specify elseif_condition if ElseIf is present. Any
number of ElseIf and elseif_conditions can be present.
Making Decisions
Nesting:
If…Then…Else block of statements can be nested within each other and also
with Select...Case statement and VBA Loops (as inner or outer loop), without
any limit.
Making Decisions
Examples of using single-line syntax for If…Then…Else Statements:
If marks > 80 Then MsgBox "Excellent Marks"
If marks > 80 Then MsgBox "Excellent Marks" Else MsgBox "Not Excellent"
'add MsgBox title "Grading":
If marks > 80 Then MsgBox "Excellent Marks", , "Grading"
Executes different blocks of code or statements, depending on the respective condition(s) being met.
It evaluates an expression and based on its result executes one of the many set of statements. It is very
similar to the If…Then…Else statements.
Syntax
Select Case expression
Case expression_value_1
statements_1
Case expression_value_n
statements_n
Case Else
else_statements
End Select
Making Decisions
Select…Case compared to If…Then…Else Statements