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



If statement  ->  In case of a multiple-line syntax (as above), the first line should have only the  If statement. See below for single-line syntax.

condition  ->  an expression (could be numeric or string)  which evaluates to True or False (note: a Null condition is equated to False). It is necessary to specify a condition.

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.

elseif_condition  [elseif_condition_1 … elseif_condition_n] ->  an expression (could be numeric or string)  which evaluates to True or False (note: a Null elseif_condition is equated to False). It is necessary to specify this if ElseIf is present.

elseif_statements  [elseif_statements_1 … elseif_statements_n] -> one or more statements (block of code) get executed if theelseif_condition evaluates to True. If elseif_statements are not specified, then no code will be executed if the elseif_condition evaluates to True.

Else  -> condition and elseif_conditions are tested in the order they are mentioned and if any one evaluates to True, its respective statements get executed and no subsequent condition is tested thereafter. If no previous condition or elseif_condition evaluates to True,Else clause comes into play and the else_statements get executed. It is Optional to include Else in the If...Then...Else statement.

else_statements  ->  one or more statements (block of code) get executed if no previous condition or elseif_condition evaluates to True. Ifelse_statements are not specified, then no code will be executed if it was applicable based on the conditions.

End If  ->  terminates the If…Then…Else block of statements and it is necessary to mention these keywords at the end.


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. It may be noted that in re. of spreadsheet functions, Excel 2003 only allows 7 levels of nesting of functions, while Excel 2007 allows up to 64

Comments

Popular posts from this blog

50 Excel VBA Oral Interview Questions