You can add ElseIf statements to test additional conditions without using nested If...Then statements, making your code shorter and easier to read. For example, suppose that you need to calculate employee bonuses using bonus rates that vary according to job classification. The following Function procedure uses a series of ElseIf statements to test the job classification before calculating the bonus.
Function Bonus(jobClass, salary, rating) If jobClass = 1 Then Bonus = salary * 0.1 * rating / 10 ElseIf jobClass = 2 Then Bonus = salary * 0.09 * rating / 10 ElseIf jobClass = 3 Then Bonus = salary * 0.07 * rating / 10 Else Bonus = 0 End If End Function
The If...Then...ElseIf statement block is very flexible. You can start with a simple If...Then statement and add Else and ElseIf clauses as necessary. However, this approach is unnecessarily tedious if each ElseIf statement compares the same expression with a different value. For this situation, you can use the Select Case statement.