|
- Sub Test()
- Dim lngI As Long
- Dim lngRows As Long
- Dim arr As Variant
- Dim objDic As Object
- Dim strKeys As Variant, strItems As Variant
- Dim arrResult As Variant
-
- Set objDic = CreateObject("Scripting.Dictionary")
-
- lngRows = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
- arr = Sheet1.Range("A1:A" & lngRows)
-
- For lngI = 1 To UBound(arr)
- '记录行号
- objDic(arr(lngI, 1)) = objDic(arr(lngI, 1)) & "," & lngI
- Next
-
- strKeys = objDic.keys
- strItems = objDic.items
- lngRows = objDic.Count
-
- ReDim arrResult(1 To lngRows, 1 To 2)
-
- For lngI = 0 To lngRows - 1
- arrResult(lngI + 1, 1) = strKeys(lngI)
- arrResult(lngI + 1, 2) = CountContinuity(CStr(strItems(lngI)))
- Next
-
-
- Sheet1.Range("C1").Resize(lngRows, 2) = arrResult
- End Sub
- Function CountContinuity(strVal As String) As Long
- Dim strT() As String
- Dim lngMaxID As Long, lngI As Long
- Dim strContinuity As String, strFind As String
- Dim lngContinuity As Long
-
- strT = Split(strVal, ",")
- lngMaxID = UBound(strT)
-
- If lngMaxID = 1 Then
- CountContinuity = 1
- Exit Function
- End If
-
- For lngI = 2 To lngMaxID
- strContinuity = strContinuity & Val(strT(lngI)) - Val(strT(lngI - 1))
- Next
-
- lngContinuity = 1
- For lngI = lngMaxID - 1 To 1 Step -1
- strFind = String(lngI, "1")
- If InStr(strContinuity, strFind) > 0 Then
- lngContinuity = lngI + 1
- Exit For
- End If
- Next
-
- CountContinuity = lngContinuity
-
- End Function
复制代码
运行效果如下:
|
评分
-
1
查看全部评分
-
|