Function SectUniteByLong(ByRef pDatas() As Long) As tpSect
Dim tOutSects() As tpSect
Dim tOutSects_Lenght As Long
ReDim tOutSects(tOutSects_Lenght)
With tOutSects(tOutSects_Lenght)
.seOn = pDatas(0)
.seEnd = pDatas(0)
End With
Dim tDatas_Index As Long
Dim tDatas_Lenght As Long
tDatas_Lenght = UBound(pDatas())
Dim tDatas_Limit As Long
Dim tDatas_Limit_Append As Boolean
Dim tDatas_Limit_Create As Boolean
For tDatas_Index = 1 To tDatas_Lenght
With tOutSects(tOutSects_Lenght)
tDatas_Limit = pDatas(tDatas_Index) - .seEnd
End With
tDatas_Limit_Append = tDatas_Limit = 1
tDatas_Limit_Create = tDatas_Limit > 1
If tDatas_Limit_Append Then
With tOutSects(tOutSects_Lenght)
.seEnd = pDatas(tDatas_Index)
End With
ElseIf tDatas_Limit_Create Then
tOutSects_Lenght = tOutSects_Lenght + 1
ReDim Preserve tOutSects(tOutSects_Lenght)
With tOutSects(tOutSects_Lenght)
.seOn = pDatas(tDatas_Index)
.seEnd = pDatas(tDatas_Index)
End With
End If
Next
举个例子
如果没有排序的话,先排一下序~~
排好之后按下面的步骤操作
arr()为事先排好的数
dim varstart ,varend ,var
dim c as long ,i as long
c=ubound(arr)
varstart=arr(0)
varend=arr(0)
for i=1 to c
var=arr(i)
if var-1=varend then
varend=var
else
if varstart=varend then
debug.print varstart & ",";
else
debug.print varstart & "-" & varend & ",";
end if
varstart=var
varend=var
end if
next
只是说明一下思路,与实际使用可能还有距离~
For j = i + 1 To UBound(data)
temp = data(i)
If data(j) < temp Then
data(i) = data(j)
data(j) = temp
End If
Next
Next
End Sub
Function CombineString(data() As Integer) As String
Dim ret As String
Dim blnContinuous As Boolean
Dim i As Integer
On Error GoTo ErrHandle
'对数组就行从小到大排序
Call Sort(data)
ret = CStr(data(LBound(data)))
For i = LBound(data) + 1 To UBound(data)
If data(i) = data(i - 1) + 1 Then
If i = UBound(data) Then ret = ret & CStr(data(i))
If Not blnContinuous Then ret = ret & "-"
blnContinuous = True
Else
If blnContinuous Then ret = ret & CStr(data(i - 1))
ret = ret & "," & CStr(data(i))
blnContinuous = False
End If
Next
CombineString = ret
Exit Function