【问题标题】:How to count series of continuous values in Excel如何在Excel中计算一系列连续值
【发布时间】:2017-04-13 08:39:06
【问题描述】:

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

现在我想知道两件事:

  1. “一组”零有多少次?所以我需要知道频率,还要计算 f.e. 20 个零为一。
  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(因为这是最长的连续零集)

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

我希望有人可以帮助我!


提前致谢, 伦斯克

【问题讨论】:

    标签: excel count binary continuous


    【解决方案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
    

    【讨论】:

    • 非常感谢 Moacir!但我猜这应该在R(或类似的)中完成呢?我希望可以在 Excel 本身中完成一些事情,也许是某种 COUNT 函数。如果这不可能,我也想知道,以便我可以调整我的工作。
    • 此解决方案涉及使用 Excel 内部的 VBA。如果您使用 Alt + F11,您应该进入代码部分。
    • 但是,如果您正在寻找“仅功能”的解决方案,我既不能给您解决方案,也不能肯定地告诉您它无法完成。
    猜你喜欢
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2014-02-24
    • 1970-01-01
    • 2019-12-07
    相关资源
    最近更新 更多