使用VBA进行分段

时间:2015-10-28 10:56:14

标签: vba

尝试Bucket数据写了这段代码,任何人都可以说这段代码有什么问题。

Function BUCKET(
    range As range, 
    x1 As Double, 
    name1 As String, 
    Optional x2 As Double, 
    Optional name2 As String, 
    Optional x3 As Double, 
    Optional name3 As String
) As String

    If range.Value < x1 Then
        BUCKET = name1
    ElseIf IsMissing(x2) Then
        If range.Value < x2 Then
            BUCKET = name2
        ElseIf IsMissing(x3) Then
            If range.Value < x3 Then
                BUCKET = name3
            Else
                BUCKET = "NO"
            End If
        End If
    End If

End Function

1 个答案:

答案 0 :(得分:0)

Function BUCKET(
    range As range, 
    x1 As Double, 
    name1 As String, 
    Optional x2 As Variant, 'IsMissing needs Variant to work.
    Optional name2 As String, 
    Optional x3 As Variant, 'IsMissing needs Variant to work. 
    Optional name3 As String
) As String

    If range.Value < x1 Then
        BUCKET = name1
    ElseIf Not IsMissing(x2) And range.Value < x2 Then
        BUCKET = name2
    ElseIf Not IsMissing(x3) And range.Value < x3 Then
        BUCKET = name3
    Else
        BUCKET = "NO"
    End If

End Function