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
In VBA, text comparison is case-sensitive by default – "pune" and "Pune" are different! That's why many students' Select Case returns "UNK". Before comparing, use Trim and LCase/UCase, or write Option Compare Text at the top of the module. ElseIf is one word – if you write "Else If", it creates a separate block.
Ravindra Bagale's Tip – मराठी
VBA मध्ये text comparison default case-sensitive असतं – "pune" आणि "Pune" वेगळे! म्हणून बऱ्याच students चा Select Case "UNK" देतो. Compare करण्याआधी Trim आणि LCase/UCase वापरा, किंवा module च्या वरती Option Compare Text लिहा. ElseIf एक शब्द आहे – "Else If" लिहिलं तर वेगळा block बनतो.
Ravindra Bagale's Tip – हिंदी
VBA में text comparison default रूप से case-sensitive होता है – "pune" और "Pune" अलग! इसीलिए बहुत से students का Select Case "UNK" देता है. Compare करने से पहले Trim और LCase/UCase इस्तेमाल करो, या module के ऊपर Option Compare Text लिखो. ElseIf एक शब्द है – "Else If" लिखा तो अलग block बन जाता है.
Worked example — if and select case
Why
Priya Deshmukh in Pune and Yuki Tanaka abroad both need a clear, repeatable way to use if and select case on a HomeNest Supplies sheet without breaking totals.
What
If And Select Case is the Excel idea this page teaches. We will walk one tiny example end to end.
How
- Open a blank sheet. In A1 type a short label for if and select case.
- Enter a small made-up value for Priya Deshmukh using £ where money appears.
- Add a second value for Yuki Tanaka using $ so you see mixed currencies stay numeric.
- Apply the method from this lesson step by step (menu or formula as shown above).
- Check the result still works inside SUM or a simple check cell.
Formula / action
Follow the formula or ribbon steps shown in this lesson (keep values numeric).
Meaning: Once if and select case is correct on one row, copy the same pattern down the column.
Practice tasks (10+)
Try these on a blank sheet. Open a hint only if you are stuck. No quiz — just practice.
1. Rebuild the smallest example of if and select case on a blank sheet for HomeNest Supplies.
Hint: Use only the steps on this page. Keep numbers numeric.
2. Change one input for Aamir Khan and confirm the result updates.
Hint: If the result is stuck, check absolute vs relative references.
3. Repeat the same steps with a £ or € value for Carlos Rivera.
Hint: Formatting must not break SUM.
4. Write the result meaning in one plain sentence next to the cell.
Hint: Imagine explaining it to a new joiners.
5. Add a second row and fill the method down.
Hint: Double-click the fill handle if the pattern is consistent.
6. Break the example on purpose, then fix it using this lesson.
Hint: Common break: text instead of a number.
7. Apply if and select case to a 5-row made-up HomeNest Supplies table.
Hint: Keep column headers clear.
8. Check your answer with a simple total or COUNT.
Hint: A wrong total usually means a format or key mismatch.
9. Swap ₹ and $ on one column and re-check totals.
Hint: Stored values stay the same; only the display changes.
10. List two mistakes beginners make with if and select case.
Hint: Look back at the tip box on this page.
11. Teach the steps out loud in under one minute.
Hint: Why → What → How.
12. Save the sheet as HomeNestSup_practice.xlsx.
Hint: Use a clear file name for class.
Loading practice tasks...
- Last used row (Easy): Which row number is the last filled row? The headings are in row 1.
- Late orders (Easy): How many rows have Status "Late"?
- Total amount (Easy): Add up the Amount column.
- Worst delay (Easy): What is the largest number in the Days late column?
- Late orders in Pune (Medium): How many orders are Late and in City "Pune"?
- Customer for an order (Medium): Find the Customer for Order ID "DL-704".
- Money stuck in late orders (Medium): Add up the Amount column for rows where Status is "Late".
- Big orders (Medium): How many rows have an Amount of 2000 or more?
- Message text (Medium): Build the text "N late orders", where N is the count of Late rows.
- Average delay of late orders (Hard): What is the average Days late for rows with Status "Late"? Round to 1 decimal.
- Tag for the first row (Hard): For the first row, return "Chase" if Days late is 3 or more, else "OK".
- Warning or all clear (Hard): Return "Warning" if any order is 3 or more days late, else "All clear".
Quick revision cards
Three standalone cards. Each one explains the idea with its own example. Scan the QR to open this lesson.


