12.9 If and Select Case
Sub ClassifyDelivery()
Dim mins As Double
mins = Worksheets("Orders").Range("H2").Value
If mins <= 10 Then
MsgBox "Excellent (10-minute delivery)"
ElseIf mins <= 15 Then
MsgBox "Good"
Else
MsgBox "Late - check with store"
End If
End Sub
Function CityCode(ByVal city As String) As String
Select Case Trim(city)
Case "Pune": CityCode = "PUN"
Case "Nashik": CityCode = "NSK"
Case "Nagpur": CityCode = "NGP"
Case "Kolhapur": CityCode = "KOP"
Case "Solapur": CityCode = "SLP"
Case "Sambhaji Nagar", "Aurangabad": CityCode = "SBN"
Case Else: CityCode = "UNK"
End Select
End Function
Select Case also accepts ranges and comparisons: Case 0 To 98, Case Is >= 499.
Ravindra Bagale's Tip
VBA madhe text comparison default case-sensitive asto – "pune" aani "Pune" vegle! Khup students cha Select Case mhanun "UNK" deto. Compare karaychya aadhi Trim aani LCase/UCase vapra, kiwa module chya varti Option Compare Text liha. ElseIf ek shabd aahe – "Else If" lihila tar vegla block banto.
Practice task
Write a macro that reads the Status in I2 and uses Select Case to colour the row green (Delivered), grey (Cancelled) or orange (Returned).