Dim i As Long
Dim t As Long
Dim count As Long
Dim final As Long
t = a(LBound(a()))
For i = LBound(a()) + 1 To UBound(a())
If a(i) = t Then
count = count + 1
Else
final = count
count = 0
t = a(i)
End If
Next
If count > final Then final = count
不好意思错了一行代码,将
if x(i)=my then
lCount1=lCount1+1
else
lCount2=lCount1
lCount1=0
end if
改为
if x(i)=my then
lCount1=lCount1+1
else
if lCount1>0 then lCount2=lCount1
lCount1=0
end if
Function countofsame(ByVal my As long, x() As long) As long
dim lCount1 as long,lCount2 as long
dim i as integer
lCount1=0
lCount2=0
For i=LBound(x()) to UBound(x())
if x(i)=my then
lCount1=lCount1+1
else
lCount2=lCount1
lCount1=0
end if
Next
if lCount2>lCount1 then
countofsame=lCount2
else
countofsame=lCount1
end if
End Function
Private Sub Command1_Click() 'check the function "countofsame"
Dim a(1000) As String
Randomize
For i = 0 To 1000
a(i) = Int(5 * Rnd)
List1.AddItem a(i) 'show the array in list1
Next
MsgBox countofsame("1", a())
End Sub
Function countofsame(ByVal mystr As String, x() As String) As Integer
countofsame = 0
Dim temp As Integer, i As Integer
i = LBound(x())
temp = 0
Do While i <= UBound(x())
If x(i) = mystr Then
temp = temp + 1
countofsame = IIf(countofsame >= temp, countofsame, temp)
Else
temp = 0
End If
i = i + 1
Loop
End Function