In: Operations Management
in VBA
What is the major benefit of using Select Case over IF statements? Provide an example of when you would use Select Case.
ANSWER :
let's say wsal is old salary & nsal is new salary of employee
Select case wsal = nsal
Case True
MsgBox "Both Sales Are Equal.", vbInformation, "Equal"
Case False
MsgBox "Both Sales Are Not Equal.", vbExclamation, "Not Equal"
End Select
If wsal= nsal Then
MsgBox "Both Sales Are Equal.", vbInformation, "Equal"
Else
MsgBox "Both Sales Are Not Equal.", vbExclamation, "Not Equal"
End if
In the first case, select case make use of a statement in the beginning to analyse the whether the values are equal or not based on the result it moves to respective true or false case.
In the second case, if case makes use of comparison in different lines. If the value is same show equal else show unequal. Thus you have to repeatedly write the if else if & if again and again to make comparisons, thus is becomes lengthy script
Thus select case makes script compact, easy understanding, comparisons made in the beginning.