'再练习一个,不用字典。效率应该也是可以的,,,
Option Explicit
Sub test()
Dim arr, m(4), p(2), i, j
arr = Range("a2:b" & ActiveSheet.UsedRange.Rows.Count).Value
ReDim brr(1 To UBound(arr, 1) * 2 + 1, 1 To 2), crr(1 To UBound(brr, 1), 1 To 4)
For j = 1 To 2
For i = 1 To UBound(arr, 1)
If Len(arr(i, j)) > 0 Then m(0) = m(0) + 1: brr(m(0), 1) = arr(i, j): brr(m(0), 2) = j
Next
Next
Call qsort(brr, 1, m(0), 1, 2, 1)
For i = 1 To m(0)
p(brr(i, 2)) = 1
If brr(i, 1) <> brr(i + 1, 1) Then
If p(1) = 1 And p(2) = 0 Then
p(1) = 1
ElseIf p(1) = 0 And p(2) = 1 Then
p(1) = 2
Else
p(1) = 3
End If
m(p(1)) = m(p(1)) + 1: crr(m(p(1)), p(1)) = brr(i, 1)
m(4) = m(4) + 1: crr(m(4), 4) = brr(i, 1)
p(1) = 0: p(2) = 0
End If
Next
[r2].Resize(UBound(crr, 1), 4) = crr
End Sub
Function qsort(arr, first, last, left, right, key)
Dim i As Long, j As Long, k As Long, x, t
i = first: j = last: x = arr((first + last) \ 2, key)
While i <= j
While arr(i, key) < x: i = i + 1: Wend
While x < arr(j, key): j = j - 1: Wend
If i <= j Then
For k = left To right
t = arr(i, k): arr(i, k) = arr(j, k): arr(j, k) = t
Next
i = i + 1: j = j - 1
End If
Wend
If first < j Then qsort arr, first, j, left, right, key
If i < last Then qsort arr, i, last, left, right, key
End Function |