2016-05-31 32 views
0

我需要從單元格A:2開始計數值,直到數值發生變化(從中計算出的單元格將具有未知數量單元格的重複值,然後是另一組值),但是初始值永遠不會相同。這是我目前擁有的代碼,直到單元格中沒有數據爲止。任何幫助,將不勝感激。VBA計數直到數值發生變化

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

'Do Until Cells(SourceRange,1)<> Cells(SourceRange + 1,1)' –

回答

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

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` 
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")) 
相關問題