<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> count1()<br><SPAN style="color:#00007F">Dim</SPAN> cell <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> Range("A2:A100")<br><SPAN style="color:#00007F">If</SPAN> cell.Value = Range("A1") <SPAN style="color:#00007F">Then</SPAN><br>i = i + 1<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">Next</SPAN><br>MsgBox "Next Count " & i<br><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> count1()<br><SPAN style="color:#00007F">Dim</SPAN> cell <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> Range("A2:A100")<br><SPAN style="color:#00007F">If</SPAN> cell.Value = Range("A1") <SPAN style="color:#00007F">Then</SPAN><br>i = i + 1<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">Next</SPAN><br><br><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> Range("A2:A100")<br><SPAN style="color:#00007F">If</SPAN> cell.Value = Range("A1") <SPAN style="color:#00007F">Then</SPAN><br>cell.Interior.Color = vbRed<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">Next</SPAN><br>MsgBox Range("A1") & " Found this many times " & i<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
' Max length of chain in series with skipping of empty values
' Cell formula: =MaxLengh(A2:A100)
' VBA call : MsgBox MaxLengh(Range("A2:A100"))
Function MaxLengh(Series) As Long
Dim arr, old, v$, x, y&
arr = Series
y = 1
For Each x In arr
v = Trim(x)
If Len(v) > 0 Then
If StrComp(v, old, vbTextCompare) = 0 Then
y = y + 1
If y > MaxLengh Then MaxLengh = y
Else
y = 1
old = x
End If
End If
Next
End Function
Sub Test_MaxLengh()
MsgBox MaxLengh(Range("A2:A100"))
End Sub
' Flag = chain length in series is >= Level, empty values are skipped
' Cell formula: =MaxLenghFlag(A2:A100,9)
' VBA call : MsgBox MaxLengh(Range("A2:A100"),9)
Function MaxLenghFlag(Series, Level) As Boolean
Dim arr, old, v$, x, y&, z&
arr = Series
y = 1
For Each x In arr
v = Trim(x)
If Len(v) > 0 Then
If StrComp(v, old, vbTextCompare) = 0 Then
y = y + 1
If y > z Then z = y
If z >= Level Then MaxLenghFlag = True: Exit For
Else
y = 1
old = x
End If
End If
Next
End Function
Sub Test_MaxLenghFlag()
MsgBox MaxLenghFlag(Range("A2:A100"), 9)
End Sub