VBA : IF…..THEN …ELSE Statement: This function enables you to check one condition and, based on that, then run one of the two statement blocks present. If the ‘IF’ condition is TRUE, IF statement and condition will run, if it is FALSE, then the ‘ELSE’ block will come in the loop. This statement will always have a closing ‘END IF’ statement else, VBA will give an error.

Syntax

Example 1: The below program checks if in ‘Sheet1’, India is present or not – if it is, then it will return ‘Delhi’ else ‘Master cell is not India’

Output

IF…..THEN …ELSE Statement in VBA

Example 2: Write a macro, checking if the candidate has scored more that 61 percentile, he/she is passed else he/she is failed.   

Output

IF THEN  ELSE Statement in VBA2
IF THEN  ELSE Statement in VBA3

Example 3: Writing a macro, checking whether the user-entered number is an even or odd number.

Output

IF THEN  ELSE Statement in VBA4
IF THEN  ELSE Statement in VBA5

Pin It on Pinterest

Share This