数到 6 后停止的功能

Function to stop after counting to 6

我有一个函数 below 计算高于中值的值,但它计算 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

如@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