2017-03-23 7 views
-4

Vor Office16 (https://support.office.com/en-us/article/SWITCH-function-47ab33c0-28ce-4530-8a45-d532ec4aa25e) gibt es keine Formel für Switch. Früher habe ich mehrere IF-Formeln oder SVERWEIS verwendet. Also ich habe eine UDF für diese Fälle erstellt, es ist in der Antwort. Dies ist meine erste UDF, ich habe sie mehrmals getestet. Wenn Sie interessiert sind, sehen Sie sich bitte an, ob es etwas zu ändern gibt.Wählen Sie Case (Switch) Formel in Excel

+0

Danke das minus ohne Kommentar :) – donmichael

+2

Es ist keine Frage, daher die unten/nahe Abstimmung. Wenn Sie möchten, dass ein funktionierender Code überprüft wird, dann veröffentlichen Sie ihn unter [Code Review] (http://codereview.stackexchange.com), nicht Stack Overflow –

Antwort

-2
Function MySwitch(ParamArray a() As Variant) 
Dim d As Integer 
Dim result As Variant 

d = UBound(a) 


myexp = a(0) 
On Error GoTo ErrHandler 
If d Mod 2 <> 0 Then 
    For i = 1 To d - 1 
     If a(i) Like ">#*" Then 
      a(i) = CInt(Replace(a(i), ">", "")) 
      Select Case myexp 
       Case Is > a(i) 
        result = a(i + 1) 
       End Select 
     ElseIf a(i) Like "<#*" Then 
      a(i) = CInt(Replace(a(i), "<", "")) 
      Select Case myexp 
       Case Is < a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "=#*" Then 
      a(i) = CInt(Replace(a(i), "=", "")) 
      Select Case myexp 
       Case a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "<>#*" Then 
      a(i) = CInt(Replace(a(i), "<>", "")) 
      Select Case myexp 
       Case Is <> a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "><#*" Then 
      a(i) = CInt(Replace(a(i), "><", "")) 
      Select Case myexp 
       Case Is <> a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "=>#*" Then 
      a(i) = CInt(Replace(a(i), "=>", "")) 
      Select Case myexp 
       Case Is >= a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like ">=#*" Then 
      a(i) = CInt(Replace(a(i), ">=", "")) 
      Select Case myexp 
       Case Is >= a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "=<#*" Then 
      a(i) = CInt(Replace(a(i), "=<", "")) 
      Select Case myexp 
       Case Is <= a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "<=#*" Then 
      a(i) = CInt(Replace(a(i), "<=", "")) 
      Select Case myexp 
       Case Is <= a(i) 
        result = a(i + 1) 
      End Select 
     Else 
      Select Case myexp 
       Case a(i) 
        result = a(i + 1) 
      End Select 
     End If 
    If Not result = vbNullString Then 
     MySwitch = result 
     Exit Function 
    End If 
    i = i + 1 
    Next i 
    result = a(d) 
ElseIf d Mod 2 = 0 Then 
    For i = 1 To d 
     If a(i) Like ">#*" Then 
      a(i) = CInt(Replace(a(i), ">", "")) 
      Select Case myexp 
       Case Is > a(i) 
        result = a(i + 1) 
       End Select 
     ElseIf a(i) Like "<#*" Then 
      a(i) = CInt(Replace(a(i), "<", "")) 
      Select Case myexp 
       Case Is < a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "=#*" Then 
      a(i) = CInt(Replace(a(i), "=", "")) 
      Select Case myexp 
       Case a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "<>#*" Then 
      a(i) = CInt(Replace(a(i), "<>", "")) 
      Select Case myexp 
       Case Is <> a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "><#*" Then 
      a(i) = CInt(Replace(a(i), "><", "")) 
      Select Case myexp 
       Case Is <> a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "=>#*" Then 
      a(i) = CInt(Replace(a(i), "=>", "")) 
      Select Case myexp 
       Case Is >= a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like ">=#*" Then 
      a(i) = CInt(Replace(a(i), ">=", "")) 
      Select Case myexp 
       Case Is >= a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "=<#*" Then 
      a(i) = CInt(Replace(a(i), "=<", "")) 
      Select Case myexp 
       Case Is <= a(i) 
        result = a(i + 1) 
      End Select 
     ElseIf a(i) Like "<=#*" Then 
      a(i) = CInt(Replace(a(i), "<=", "")) 
      Select Case myexp 
       Case Is <= a(i) 
        result = a(i + 1) 
      End Select 
     Else 
      Select Case myexp 
       Case a(i) 
        result = a(i + 1) 
      End Select 
     End If 
    If Not result = vbNullString Then 
     MySwitch = result 
     Exit Function 
    End If 
    i = i + 1 
    Next i 
End If 

MySwitch = result 
Exit Function 
ErrHandler: 
If Err.Number <> 0 Then 
    Msg = "Error # " & Str(Err.Number) & " was generated by " _ 
     & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description 
    MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext 
MySwitch = Err.Description 
End If 
End Function