VBA计数直到值发生变化

时间:2016-05-31 18:47:28

标签: excel vba excel-vba

我需要从单元格A:2中计算值,直到值发生变化(我将计算的表格将具有未知数量的单元格的重复值,然后是另一组值),但是最初的值价值永远不会是一样的。这是我目前拥有的代码,直到单元格中没有数据为止。任何帮助将不胜感激。

            SourceRange = 1
            Do Until Cells(SourceRange, 1) = ""
                SourceRange = SourceRange + 1

3 个答案:

答案 0 :(得分:0)

怎么样:

Sub dural()
    v = Range("A2").Value
    For i = 3 To Rows.Count
        If Cells(i, 1).Value <> v Then
            MsgBox CStr(i - 2)
            Exit Sub
        End If
    Next i
End Sub

enter image description here

答案 1 :(得分:0)

试试这个

Sub CountSame()

    Dim OC As Range, CC As Range

    Set OC = Range("a2")
    Set CC = OC

    Do Until CC.Value <> OC.Value
       Set CC = CC.Offset(1, 0)
       a = a + 1
    Loop

    MsgBox a

End Sub`

答案 2 :(得分:0)

您可以使用

Function CountConsecutiveSame(rng as Range) as Long
    With Rng
        Set found = Range(.Offset(1), .End(xlDown)).Find(What:=.Value, LookAt:=xlWhole, LookIn:=xlValues)
        If Not found Is Nothing Then CountConsecutiveSame = found.Row - .Row + 1
    End With
End Function

一样使用
MsgBox CountConsecutiveSame(Range("A2"))