计数到6后停止的功能

时间:2016-09-21 12:22:25

标签: vba function count median

我有一个函数,低于计算高于中值的值,但它计算A中高于中位数的所有值。我需要这个,当它达到6时停止计数。任何帮助都会很棒。

Function CountAbove(RangeToCountAbove As Range, _
                MedianOfLastGroup As Double) As Long

Dim i As Double
Dim rows As Double
Dim cCell As Range

CountAbove = 0

For Each cCell In RangeToCountAbove
If (cCell.Value > MedianOfLastGroup) Then
    CountAbove = CountAbove + 1
Else
Exit Function
End If
Next cCell

End Function

1 个答案:

答案 0 :(得分:1)

正如@Comintern所写,将条件If CountAbove = 6放在If中,如下面编辑的代码所示:

Function CountAbove(RangeToCountAbove As Range, _
                MedianOfLastGroup As Double) As Long

Dim i As Double
Dim rows As Double
Dim cCell As Range

CountAbove = 0

For Each cCell In RangeToCountAbove
    If (cCell.Value > MedianOfLastGroup) Then
        CountAbove = CountAbove + 1
        If CountAbove = 6 Then Exit Function
    Else
        Exit Function
    End If

Next cCell

End Function