spot_img

Nested If Statement

An If or ElseIf statement inside another If or ElseIf statement. The inner If statements execution is based on the outermost If statements. This enables VBScript to handle complicated conditions.

Syntax

Flow Diagram

Nested If Statement

In a nested statement, VBA runs through each of the If…Then conditions even after evaluate a True condition (and executing its associated statements), whereas in an ElseIf structure all following conditions are skipped after evaluating a True condition.

In this situation, the ElseIf structure is faster. Thus, nested statements might not include a very efficient method if the same can be done with the ElseIf structure.

For example: suppose you want to write a code that returns the message excellent if a student mark is greater or equal to the 80. And returns good, average, and poor according to the decision criteria as shown in the below example, such as:

Nested If Statement

Run the above code in VBA, and you will get the output as shown in the below screenshot.

Nested If Statement

Example

Let’s find the positive numbers of Excel with the help of a function.

After that execution of the above code, you will get the output such as:

A number is a positive number  The number is not 0, 1, 2, or 3 

spot_img
Previous articleMac Excel Shortcuts
Next articleFeatures of MS Excel