用VBA数组或字典计算A列数字数字连续次数

用VBA数组或字典计算A列数字数字连续次数
2025-02-23 10:19:06
推荐回答(2个)
回答1:

Sub s()
    arr = [a1].CurrentRegion
    Set d = CreateObject("scripting.dictionary")
    k = 1
    For i = 2 To UBound(arr)
        If arr(i, 1) = arr(i - 1, 1) Then
            k = k + 1
        Else
            If d(arr(i - 1, 1)) < k Then d(arr(i - 1, 1)) = k
            k = 1
        End If
    Next
    If d(arr(i - 1, 1)) < k Then d(arr(i - 1, 1)) = k
    [c1].Resize(d.Count) = Application.Transpose(d.keys)
    [d1].Resize(d.Count) = Application.Transpose(d.items)
End Sub

回答2:

你可以利用公式countif函数,如果一定要用VBA的话,那就把公式放到VBA里面去写出来