
'如果不存在指定key的数据将按原序放最后
Option Explicit
Sub abc()
Dim i, a, d
Set d = CreateObject("scripting.dictionary")
a = [g1].CurrentRegion.Offset(1).Value
For i = 1 To UBound(a) - 1
d(a(i, 1)) = i
Next
a = [a1].CurrentRegion.Resize(, 3).Value
For i = 2 To UBound(a)
If d.exists(a(i, 1)) Then a(i, 3) = d(a(i, 1)) Else a(i, 3) = d.Count + 1
Next
Call bsort(a, 2, UBound(a), 1, 3, 3)
[d1].Resize(UBound(a), 2) = a
End Sub
Function bsort(a, first, last, left, right, key)
Dim i, j, k, t
For i = first To last - 1
For j = first To last + first - 1 - i
If a(j, key) > a(j + 1, key) Then
For k = left To right
t = a(j, k): a(j, k) = a(j + 1, k): a(j + 1, k) = t
Next
End If
Next
Next
End Function

菠萝蜜