Option Explicit
Sub test()
Dim arr, i, j, dic, cnt, m, brr, temp
Set dic = CreateObject("scripting.dictionary")
arr = Range("a2:a" & Cells(Rows.Count, "a").End(xlUp).Row)
For i = 1 To UBound(arr, 1): dic(arr(i, 1)) = i: Next
arr = Range("b2:c" & Cells(Rows.Count, "b").End(xlUp).Row)
brr = arr: temp = brr
For i = 1 To UBound(arr, 1)
If dic.exists(arr(i, 1)) Then
cnt = cnt + 1
For j = 1 To UBound(arr, 2): brr(cnt, j) = arr(i, j): Next
End If
Next
m = cnt
For i = 1 To UBound(arr, 1)
If Not dic.exists(arr(i, 1)) Then
m = m + 1
For j = 1 To UBound(arr, 2): brr(m, j) = arr(i, j): Next
End If
Next
Call msort(brr, temp, 1, cnt, 1, UBound(arr, 2), 1, dic)
[e2].Resize(UBound(brr, 1), UBound(brr, 2)) = brr
End Sub
Function msort(arr, temp, first, last, left, right, key, dic)
Dim i, j, k, kk, mid
If first <> last Then
mid = Int((first + last) / 2)
msort arr, temp, first, mid, left, right, key, dic
msort arr, temp, mid + 1, last, left, right, key, dic
i = first: j = mid + 1: k = first
While i <= mid And j <= last
If dic(arr(i, key)) <= dic(arr(j, key)) Then
For kk = left To right: temp(k, kk) = arr(i, kk): Next
k = k + 1: i = i + 1
Else
For kk = left To right: temp(k, kk) = arr(j, kk): Next
k = k + 1: j = j + 1
End If
Wend
While i <= mid
For kk = left To right: temp(k, kk) = arr(i, kk): Next
k = k + 1: i = i + 1
Wend
While j <= last
For kk = left To right: temp(k, kk) = arr(j, kk): Next
k = k + 1: j = j + 1
Wend
For i = first To last
For j = left To right
arr(i, j) = temp(i, j)
Next j, i
End If
End Function |