[英]How to count series of continuous values in Excel
我希望您能帮助我解决这个(相对)简单的问题。.我在Excel中有一个数据集,其中的一个列(例如A5:A605)是一系列二进制值-只有一个和零。
现在我想知道两件事:
为了使它更加清晰,以下范围:
0 0 0 0 1 1 0 1 1 1 0 0 1 0 0 0 0 0 1 1
应该给我答案:
作为奖励,如果它还可以指示最长连续集在哪里,那会很好,但这不是完全必要的。
我希望有一个人可以帮助我!
这样行吗?
Sub marine()
Dim row As Long
Dim countstreak As Long
Dim count As Long
Dim col As Long
Dim nzeroes As Long
Dim lzero As Long
Dim nzeroescol As Long
Dim lzerocol As Long
Dim lastrow As Long
Dim i As Long
'Configuration
col = 1 '1 is for column A, change here in case you need
nzeroescol = 2 'Which column it will show the number of sets of zeroes
lzerocol = 3 'Which column it will show the longest set of zeroes
lastrow = 605 'Last row of your data. You can make it automatic by changing 605 to "Cells(Rows.count,col).end(xlup).row" no quotes
For row = 5 To lastrow 'Change the 5 to the starting row of your data if needed
For i = 1 To Len(Cells(row, col))
If Mid(Cells(row, col), i, 1) = "0" Then
count = 1
countstreak = countstreak + 1
ElseIf Mid(Cells(row, col), i, 1) = "1" Then
nzeroes = nzeroes + count
If countstreak > lzero Then
lzero = countstreak
End If
countstreak = 0
count = 0
End If
Next i
Cells(row, nzeroescol) = nzeroes
Cells(row, lzerocol) = lzero
nzeroes = 0
lzero = 0
Next row
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.