Vba代码不能连续计数1秒

时间:2017-10-27 14:40:49

标签: excel vba excel-vba

在excel中,我有一堆由1和0序列组成的列。对于每一列,我希望计算该列中有4个或更多1个连续1的次数。我相信一段vba代码是最简单的方法吗?

示例:

0
1
1
1
1
0
1
1
1
1
1
0

0

此处的计数为2。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用此功能:

    Function Count1s(rng As Range) As Long
Dim rArr() As Variant
Dim i As Long
Dim t As Long
Dim n As Long
t = 0
rArr = rng.Value

For i = 1 To UBound(rArr, 1)
    If rArr(i, 1) <> 1 Then
        If t + 4 <= i Then
            n = n + 1
        End If
        t = i
    End If
Next i
Count1s = n
End Function

您可以将其称为常规公式:

=Count1s(A1:A12)

enter image description here