計算序列中的重複值 - 對於足球比分

計算序列中的重複值 - 對於足球比分

我目前有一列類似下面的數值,列出了足球比賽的結果。

Lost
Lost
Lost
Won
Drew
Drew
Won
Won

使用這個公式,我已經能夠計算出序列重複的位置 - 因此對於上面的列表,連續有三個“丟失”。

=IF(A2=A1,B1+1,1)

但是,我想計算值是“Drew”或“Won”的位置(從而顯示沒有出現“lost”的行數。這可能嗎?

謝謝

答案1

這是一個模組子模組,它遍歷 A 列,直到遇到空白單元格,並將最大的非「遺失」條紋列印到單元格 B1。

Sub CountNonLoss()

Dim nonloss As Integer
Dim LongestStreak As Integer
Dim Val
Dim streak As New Collection
nonloss = 0
LongestStreak = 0

Range("A1").Select

  Do Until IsEmpty(ActiveCell)
    If ActiveCell.Value <> "Lost" Then
       nonloss = nonloss + 1
    Else
       streak.Add nonloss
       nonloss = 0
    End If
  ActiveCell.Offset(1, 0).Select
  Loop

  For Each Val In streak
    If Val > LongestStreak Then
       LongestStreak = Val
    End If
  Next Val

Range("B1").Value = LongestStreak
End Sub

相關內容