Sub ct()
Dim r, n, str As String
r = Range("a65536").End(xlUp).Row
n = 0
str = "2在A列连续出现的次数为:"
For i = 1 To r
If Cells(i, 1) = 2 Then
If Cells(i + 1, 1) = 2 Then
n = n + 1
Else
n = n + 1
str = str & " " & n & "次"
n = 0
End If
End If
Next i
MsgBox str
End Sub
运行结果如图:
这个要VBA才可以实现
比如2
Sub tongji()
k = 1
j = 0
If Cells(1, 1) = 2 Then
j = 1
Else
j = 0
End If
For i = 2 To Cells(65000, 1).End(xlUp).Row + 1
If Cells(i, 1) = 2 Then
j = j + 1
Else
If j > 0 Then
Cells(k, 2) = j
k = k + 1
End If
j = 0
End If
Next i
End Sub