我希望你可以帮我解决这个(相对)简单的问题。我在Excel中有一个数据集,其中一列,让我们说A5:A605,是一系列二进制值 - 只有1和0 。
现在我想知道两件事:
为了使它更清晰,可以使用以下范围:
0 0 0 0 1 1 0 1 1 1 0 0 1 0 0 0 0 0 1 1
应该给我答案:
作为奖励,如果它还可以指示最长连续集的位置,那将是很好的,但这不是完全必要的。
我希望有人可以帮助我!
<小时/> 提前致谢, 伦斯克
答案 0 :(得分:0)
这会有用吗?
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