Option Explicit
'找出中间不连续的---结果 5-12,2-4,6-8,12-23,13,15,19-21,22-24,26-29 Sub asdf1001_1()
Dim lstarr() As Variant, lstrng As Range, lstcount As Long
Set lstrng = Sheet1.Columns(1) lstcount = Application.CountA(lstrng) ReDim lstarr(1 To lstcount, 1 To 1) lstarr = lstrng.Resize(lstcount).Value
Dim i As Long Dim tmpstr As String For i = 2 To lstcount Select Case lstarr(i, 1) - lstarr(i - 1, 1) Case Is > 2: tmpstr = tmpstr & lstarr(i - 1, 1) + 1 & "-" & lstarr(i, 1) - 1 & "," Case Is > 1: tmpstr = tmpstr & lstarr(i, 1) - 1 & "," End Select Next i Debug.Print Left$(tmpstr, Len(tmpstr) - 1) End Sub
'找出自始至终未出现的---结果 6-8,15,19,20,26-29 Sub asdf1001_2()
Dim lstarr() As Variant, lstrng As Range, lstcount As Long Dim minnum As Integer, maxnum As Integer
Set lstrng = Sheet1.Columns(1) lstcount = Application.CountA(lstrng) minnum = Application.Min(lstrng) maxnum = Application.Max(lstrng) ReDim lstarr(1 To lstcount, 1 To 1) lstarr = lstrng.Resize(lstcount).Value
Dim i As Integer, j As Long Dim tmpstr As String Dim exist_flag As Boolean
For i = minnum To maxnum exist_flag = False For j = 1 To lstcount If lstarr(j, 1) = i Then exist_flag = True: Exit For Next j If exist_flag = False Then tmpstr = tmpstr & i & "," Next i Dim tmparr() As String, tmp() As String
tmparr = Split(Left$(tmpstr, Len(tmpstr) - 1), ",") tmp = tmparr For i = LBound(tmparr) + 1 To UBound(tmparr) - 1 If (tmparr(i) - tmparr(i - 1)) * (tmparr(i + 1) - tmparr(i)) = 1 Then tmp(i) = "-" Next tmpstr = Join$(tmp, ",") Do Until InStr(1, tmpstr, "-,") + InStr(1, tmpstr, ",-") + InStr(1, tmpstr, "--") = 0 tmpstr = Replace$(tmpstr, "-,", "-") tmpstr = Replace$(tmpstr, ",-", "-") tmpstr = Replace$(tmpstr, "--", "-") Loop Debug.Print tmpstr End Sub
|