IF Statement is a powerful tool in Excel VBA. Many logical operation being done through IF Statement. The basic term of IF Statement is doing something if the given logic is TRUE. The diagram of IF Statement are 3 types:
(a) Easy IF Statement, where Logic is TRUE only:
Now type the below code in your Excel VBA:
(a) Easy IF Statement, where Logic is TRUE only:
Image 1: Easy IF Statement
(b) Medium IF Statement, where Logic is TRUE or FALSE:
Image 2: Medium IF Statement
(c) Nested IF Statement, Where another IF assigned under a logic:
Image 3: Nested IF Statement
Generally the above 3 types of IF Statement are found in Excel VBA. Remember all your IF Statement depends on 2 important features. These are Conditional Operators and Logical Operators. These are described as below:
1) Conditional Operators:
To write logic in IF Statement you must use the Conditional Operators like, = (equal to). You can apply below 6 types of Conditional Operators. These are:
Image 4: Conditional Operators
2) Logical Operators:
To write 2 or 3 logic in IF Statement you must apply Logical Operator such as AND. You can apply below 4 types of Logical Operators:
Image 5: Logical Operators
Example 1: Student Grade Sheet
This example will cover Logical Operators, Conditional Operators and Medium IF Statement. However, you can understand how a Loop used in Excel VBA.
Assume that below is the result sheet page format. Already students are achieved their Marks in between 1 to 100 in 5 different Excel subjects. Now calculate their result in Grade System based on the Grade Chart given:
Image 6: Students result sheet
Now type the below code in your Excel VBA:
Image 7: Student result sheet code
Now click the Generate Result button in Excel worksheet. This will show you the result:
Image 8: Result