先建一个类模块,粘贴下面代码:
Option Explicit
Private mCount As Integer
''''''相同数的个数'''''''
Public Property Get Count() As Integer
Count = mCount
End Property
''''''''''求几个相同数'''''''
Public Function SameNumber(StringValue As String) As String
Dim Value() As String
Dim ColNum As Collection
'''存储数字
Dim i As Integer
Dim j As Integer
Dim Q As Integer
Dim Q1 As Integer
Dim Temp As Integer
Dim Temp1 As Integer
Dim Temp2 As String
On Error GoTo ProcError
Value() = Split(StringValue, ",")
Q = LBound(Value)
Q1 = UBound(Value)
''''''先排序,从小到大'''''
For i = Q To Q1 - 1
'
Temp = Val(Value(i))
For j = i + 1 To Q1
'
Temp1 = Val(Value(j))
If Val(Value(i)) > Val(Value(j)) Then
'
If Temp > Temp1 Then
Temp2 = Value(j)
Value(j) = Value(i)
Value(i) = Temp2
End If
Next
Next
'
Temp2 = ""
'
For i = Q To Q1
'
If i = Q Then
'
Temp2 = Value(i)
'
'
Else
'
Temp2 = Value(i) & "," & Temp2
'
'
End If
'
'
Next
'
MsgBox "排序后的序列为:" & Temp2
'''''''取出不同的数'''''''''''
Set ColNum = New Collection
ColNum.Add Val(Value(Q))
Temp = Val(Value(Q))
For j = Q + 1 To Q1
Temp1 = Val(Value(j))
If Temp <> Temp1 Then
ColNum.Add Temp1
Temp = Temp1
End If
Next
Dim x As Variant
Temp2 = ""
For Each x In ColNum
If Temp2 = "" Then
Temp2 = x
Else
Temp2 = Temp2 & "," & x
End If
Next
'
MsgBox "取出的数列为:" & Temp2
SameNumber = Temp2
mCount = ColNum.Count
ProcExit:
Exit Function
ProcError:
MsgBox Err.Number & vbCrLf & Err.Description, 48
Resume ProcExit
End Function
''''''判断是否为连续的数列'''''''
Public Function IsSame(StringValue As String) As Boolean
Dim Temp As String
Dim Temp1 As Integer
Dim x As Variant
Dim Value() As String
''''''获取不相同的数列'''''''''
Temp = Me.SameNumber(StringValue)
Value() = Split(Temp, ",")
IsSame = True
Temp1 = -1
For Each x In Value
If Temp1 = -1 Then
Temp1 = Val(x)
Else
If Val(x) - Temp1 <> 1 Then
IsSame = False
Exit For
Else
Temp1 = Val(x)
End If
End If
Next
End Function
然后在Form1窗体上建Text1,Text2,text3,text4和一个command1按钮,Text1为输入框。然后粘贴下列代码:
Option Explicit
Private Sub Command1_Click()
Dim C As New Class1
Text3 = C.SameNumber(Text1)
Text2 = C.Count
If C.IsSame(Text1) = False Then
Text4 = "不是连续的数列!"
Else
Text4 = "此数列为连续的数列!"
End If
End Sub