本帖最后由 bajifeng 于 2016-1-1 12:40 编辑
烧脑子。。。
- Sub db() 'bajifeng
- arr = Array(2, 3, 6, 9, 11, 12, 13, 14, 15, 16, 19, 25, 26, 27, 101, 105, 106, 107, 109)
- For i = 1 To UBound(arr) - 1
- 'Debug.Print arr(i)
- If arr(i) - arr(i - 1) = 1 Then
- n = n + 1
- If n = 1 Then
- sl = arr(i - 1) & "-"
- End If
- If arr(i + 1) - arr(i) = 1 Then
- GoTo 100
- Else
- sl = sl & arr(i) & ","
- n = 0
- End If
- Else
- If arr(i + 1) - arr(i) = 1 Then GoTo 100
- sl = arr(i) & ","
- End If
- 100:
- s = s & sl
- sl = ""
- Next
- If Right(s, 1) = "," And arr(UBound(arr)) - arr(UBound(arr) - 1) = 1 Then
- s = Left(s, Len(s) - 1 - Len(arr(UBound(arr)))) & arr(UBound(arr))
- Else
- s = s & arr(UBound(arr))
- End If
- MsgBox s
- End Sub
复制代码
|