VBAtoPython

VBA Select Case to Python match/case

VBA's Select Case is the go-to for multi-branch logic in Excel macros. Python 3.10 introduced structural pattern matching with match/case, while earlier versions use if/elif chains.

VBA Select Case Example

VBA

Function CalculateDiscount(customerType As String, orderAmount As Double) As Double
    ' Calculate discount based on customer type and order amount
    Dim discountRate As Double
    
    Select Case customerType
        Case "Premium"
            Select Case True
                Case orderAmount >= 1000
                    discountRate = 0.15
                Case orderAmount >= 500
                    discountRate = 0.12
                Case Else
                    discountRate = 0.1
            End Select
        Case "Regular", "Standard"
            Select Case True
                Case orderAmount >= 1000
                    discountRate = 0.1
                Case orderAmount >= 500
                    discountRate = 0.08
                Case Else
                    discountRate = 0.05
            End Select
        Case "New"
            discountRate = 0.03 ' Welcome discount
        Case Else
            discountRate = 0 ' No discount for unknown types
    End Select
    
    CalculateDiscount = orderAmount * discountRate
End Function

Python match/case Equivalent

Python

def calculate_discount(customer_type: str, order_amount: float) -> float:
    """Calculate discount based on customer type and order amount."""
    
    match customer_type:
        case "Premium":
            match True:
                case _ if order_amount >= 1000:
                    discount_rate = 0.15
                case _ if order_amount >= 500:
                    discount_rate = 0.12
                case _:
                    discount_rate = 0.1
        case "Regular" | "Standard":
            match True:
                case _ if order_amount >= 1000:
                    discount_rate = 0.1
                case _ if order_amount >= 500:
                    discount_rate = 0.08
                case _:
                    discount_rate = 0.05
        case "New":
            discount_rate = 0.03  # Welcome discount
        case _:
            discount_rate = 0.0  # No discount for unknown types
    
    return order_amount * discount_rate

Common VBA → Python Mappings

VBA Select CasePython match/case
Select Case variablematch variable:
Case "value"case "value":
Case 1, 3, 5case 1 | 3 | 5:
Case Is > 100case x if x > 100:
Case Elsecase _:
Select Case Truematch True:

Side-by-Side Comparison

VBA

Select Case grade
    Case 90 To 100
        letterGrade = "A"
    Case 80 To 89
        letterGrade = "B"
    Case 70 To 79
        letterGrade = "C"
    Case Else
        letterGrade = "F"
End Select

Python

match grade:
    case g if 90 <= g <= 100:
        letter_grade = "A"
    case g if 80 <= g <= 89:
        letter_grade = "B"
    case g if 70 <= g <= 79:
        letter_grade = "C"
    case _:
        letter_grade = "F"

Important Differences

  • Python Version Requirement: match/case requires Python 3.10+. For older versions, use if/elif chains or dictionary dispatch patterns instead.
  • Guard Clause Syntax: VBA uses 'Case Is > 5' while Python uses 'case x if x > 5:' with explicit variable binding in guard conditions.
  • Multiple Value Syntax: VBA separates multiple values with commas 'Case 1, 3, 5' while Python uses pipe operator 'case 1 | 3 | 5:'.
  • Variable Binding: Python match can bind variables in patterns, while VBA Select Case only evaluates expressions against the test value.

Have a VBA macro full of Select Case blocks?

Paste it into the converter to get clean Python match/case statements instantly.