Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Excel VBA

Introduction
Making Decisions
Conditional Statements

There are primarily two Conditional Statements in VBA: If…Then…Else


Statements and Select…Case Statement.

In both of these, one or more conditions are evaluated and a block of


code is executed depending on the result of the evaluation.
Making Decisions
If...Then...Else Statements
Executes a block of code or statements, if the specified condition is met.

Single-line If...Then...Else Statements

Syntax (single-line):

If condition Then statements Else else_statements


 
If…Then…Else block of statements can be nested within each other in a single-line syntax also.
 
A clause similar to ElseIf (in multiple-line syntax) can be inserted by using the keywords Else If (in single-line
syntax).
 
End If keywords are not required to end the procedure, in the single-line syntax.
 
Making Decisions
Multiple-line statements  -  Syntax
If condition Then
statements
ElseIf elseif_condition_1 Then
elseif_statements_1
ElseIf elseif_condition_n Then
elseif_statements_n
Else
else_statements
End If
Making Decisions
If statement  ->  In case of a multiple-line syntax (as above), the first line should
have only the  If statement.

condition  ->  an expression (could be numeric or string)  which evaluates to


True or False

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"

'using logical operator And in the condition:


If marks > 80 And avg > 80 Then MsgBox "Both Marks & Average are Excellent" Else MsgBox "Not Excellent"
 
'nesting another If...Then statement:
If marks > 80 Then If avg > 80 Then MsgBox "Both Marks & Average are Excellent"
Making Decisions
Select...Case Statement  

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

Both are Conditional Statements, wherein one or more conditions are


evaluated and a block of code is executed depending on the result of the
evaluation.

The difference lies in that in a Select…Case statement, a single expression is


considered and evaluated at a time.

The variable to be evaluated is determined in the first line of "Select Case


expression", and then multiple Case statements specify the possible values.
Making Decisions
Whereas in If…Then…Else statements, multiple expressions can be
considered and evaluated simultaneously.

Select...Case statement tests a single item for several possible values,


whereas If...Then...Else statements test multiple items for several possible
values.

In this sense, If...Then...Else statements are more flexible in testing multiple


variables for multiple conditions.

You might also like