繁体   English   中英

如何在Excel中计算一系列连续值

[英]How to count series of continuous values in Excel

我希望您能帮助我解决这个(相对)简单的问题。.我在Excel中有一个数据集,其中的一个列(例如A5:A605)是一系列二进制值-只有一个和零。

现在我想知道两件事:

  1. 零的“集合”有多少次? 因此,我需要知道频率,还要将fe 20个零算为1。
  2. 最长的零“集”是什么?

为了使它更加清晰,以下范围:

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

应该给我答案:

  1. 4 (因为有四组零)和
  2. 5 (因为这是最长的连续零集)

作为奖励,如果它还可以指示最长连续集在哪里,那会很好,但这不是完全必要的。

我希望有一个人可以帮助我!


在此先感谢Renske

这样行吗?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM