|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test()
- Dim r%, i%, m%
- Dim arr, brr, zrr()
- With Worksheets("题目")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:a" & r)
- ReDim brr(1 To UBound(arr), 1 To 1)
- For i = 1 To UBound(arr)
- xm = Split(arr(i, 1), ",")
- m = 1
- ReDim zrr(1 To m)
- zrr(m) = Array(0, 0, -1)
- For j = 1 To UBound(xm)
- If Val(xm(j)) = Val(xm(j - 1)) + 1 Then
- If zrr(m)(2) = -1 Or zrr(m)(2) = 1 Then
- zrr(m)(1) = j
- zrr(m)(2) = 1
- Else
- m = m + 1
- ReDim Preserve zrr(1 To m)
- zrr(m) = Array(j, j, -1)
- End If
- ElseIf Val(xm(j)) = Val(xm(j - 1)) + 2 Then
- If zrr(m)(2) = -1 Or zrr(m)(2) = 2 Then
- zrr(m)(1) = j
- zrr(m)(2) = 2
- Else
- m = m + 1
- ReDim Preserve zrr(1 To m)
- zrr(m) = Array(j, j, -1)
- End If
- Else
- m = m + 1
- ReDim Preserve zrr(1 To m)
- zrr(m) = Array(j, j, -1)
- End If
- Next
- For k = 1 To UBound(zrr)
- If zrr(k)(0) = zrr(k)(1) Then
- brr(i, 1) = brr(i, 1) & "," & xm(zrr(k)(0))
- Else
- If zrr(k)(2) = 1 Then
- brr(i, 1) = brr(i, 1) & "," & xm(zrr(k)(0)) & "-" & xm(zrr(k)(1))
- Else
- If xm(zrr(k)(0)) Mod 2 = 1 Then
- brr(i, 1) = brr(i, 1) & "," & xm(zrr(k)(0)) & "-" & xm(zrr(k)(1)) & "(单)"
- Else
- brr(i, 1) = brr(i, 1) & "," & xm(zrr(k)(0)) & "-" & xm(zrr(k)(1)) & "(双)"
- End If
- End If
- End If
- Next
- Next
- For i = 1 To UBound(brr)
- If brr(i, 1) <> Empty Then
- brr(i, 1) = Mid(brr(i, 1), 2)
- End If
- Next
- .Range("c2").Resize(UBound(brr), 1) = brr
- End With
- End Sub
复制代码 |
|