请问EXCEL如何统计一列中连续出现相同数字的次数?

详情如图,谢谢老师 例如,我想统计A列中连续出现三次(不包括三次以上)-1的总次数,

用宏代码吧:

Sub www()
    Dim i%, s1%, s2%, s3%, s4%, s5%, s6%
    For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(i - 1, 1) <> -1 And Cells(i, 1) = -1 And Cells(i + 1, 1) = -1 And Cells(i + 2, 1) <> -1 Then s1 = s1 + 1
        If Cells(i - 1, 1) <> -1 And Cells(i, 1) = -1 And Cells(i + 1, 1) = -1 And Cells(i + 2, 1) = -1 And Cells(i + 3, 1) <> -1 Then s2 = s2 + 1
        If Cells(i - 1, 1) <> -1 And Cells(i, 1) = -1 And Cells(i + 1, 1) = -1 And Cells(i + 2, 1) = -1 And Cells(i + 3, 1) = -1 And Cells(i + 4, 1) <> -1 Then s3 = s3 + 1
        
        If Cells(i - 1, 1) <> 1 And Cells(i, 1) = 1 And Cells(i + 1, 1) = 1 And Cells(i + 2, 1) <> 1 Then s4 = s4 + 1
        If Cells(i - 1, 1) <> 1 And Cells(i, 1) = 1 And Cells(i + 1, 1) = 1 And Cells(i + 2, 1) = 1 And Cells(i + 3, 1) <> 1 Then s5 = s5 + 1
        If Cells(i - 1, 1) <> 1 And Cells(i, 1) = 1 And Cells(i + 1, 1) = 1 And Cells(i + 2, 1) = 1 And Cells(i + 3, 1) = 1 And Cells(i + 4, 1) <> 1 Then s6 = s6 + 1
    Next i

    [d2] = s1  '-1连2次
    [d4] = s2  '-1连3次
    [d6] = s3  '-1连4次
    [d8] = s4  '1连2次
    [d10] = s5  '1连3次
    [d12] = s6  '1连4次

End Sub

追问

谢谢您。请问以此类推连续连续5-6-7-8 在那个位置编辑,还有这个不会算重复吧?例如四个-1连续,它会不会显示出连续三次-1出现两次的情况

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-09-16
=countif(a:a,a1)追问

不是的,例如是要统计A列-1-1-1三次连续出现的总次数而且不包含连续4期的、

相似回答