2017-03-23 6 views
-4

Office16 (https://support.office.com/en-us/article/SWITCH-function-47ab33c0-28ce-4530-8a45-d532ec4aa25e) 이전에는 스위치 수식이 없습니다. 여러 개의 IF 수식 또는 VLOOKUP을 사용했습니다. 그래서 저는이 경우에 대해 UDF를 만들었습니다. 이것은 첫 번째 UDF이므로 여러 번 테스트했습니다. 만약 당신이 interesed보세요, 그리고 저에게, 수정할 게 있다면 말해.Excel (Excel)에서 사례 (스위치) 수식을 선택하십시오.

+0

주석없이 마이너스를 보내 주셔서 감사합니다. – donmichael

+2

이것은 질문이 아니므로 아래/닫기 투표입니다. 일부 작업 코드를 검토하려면 - [코드 검토] (http://codereview.stackexchange.com)에 게시하십시오 (스택 오버플로가 아님). –

답변

-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