If Then Else
- 02:28
How If Then Else can be used to add optionality in procedures.
Downloads
No associated resources to download.
Transcript
The instructions inside a looping procedure can be made more sophisticated by adding some optionality if then else works in a similar way to the Excel function if and allows for alternative instructions to be carried out.
If a condition is met, the syntax when using if then else requires us to begin our code with If, a condition to be tested is then provided, followed immediately by then note the if and then must always be on the same line of code.
The next line of our code provides the instructions to carry out if the condition is true.
If the condition is found not to be true else, if provides a further condition to test followed by then the next line of code provides the instructions to carry out if this further condition is found to be true.
Else is the syntax required to direct the instruction? If this further condition is found not to be true, the if then else instructions are completed with an end if statement.
If we do not want to consider alternative conditions and simply require an instruction to be performed, if a condition is found to be true, we can use an if then statement in the example, a condition that the cell range variable is tested to see if it meets the condition of being less than zero.
Then if the condition is met, there is a line of code with instructions to assign the colour index property of the interior of the cell, a value of 3 or red. Note, If the condition is not met, nothing happens.
And if completes the if then statement, we can see that the if then statement is within a four each next loop so that each cell in the range can be evaluated with our if then else coding.