Excel · मराठी आवृत्ती
12.9 If आणि Select Case
If conditionsनुसार वेगळं काम करतो. Select Case एका valueचे अनेक cases वाचायला सोप्या पद्धतीने हाताळतो.
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
Delivery exampleमध्ये ≤10 Excellent, ≤15 Good, बाकी Late. पण source cell blank, text किंवा error असेल तर आधी validate करा; blankला zero मानून Excellent म्हणू नका. Worksheetsला ThisWorkbook qualifier द्या.
CityCodeमध्ये Trim leading/trailing spaces काढतो. Default binary text comparison case-sensitive असू शकतो; pune आणि Puneसाठी same code हवा असेल तर Select Case LCase(Trim(city)) आणि lowercase Case labels वापरा. Option Compare Text हा module-level पर्याय आहे.
Select Caseमध्ये Case 0 To 98 किंवा Case Is >= 499 देता येतं. Decimal amountsसाठी 98 ते 99मधला gap होऊ नये म्हणून threshold rules नीट ठरवा. ElseIf एक शब्द आहे.
Practice
I2चा Status normalize करून Delivered → green, Cancelled → grey, Returned → orange. Unknown/blankसाठी वेगळा flag ठेवा; default Delivered करू नका. Colourसोबत text statusही ठेवा.