'A列可以无序,,,
Option Explicit
Sub test()
Dim arr, i, p, dic
Set dic = CreateObject("scripting.dictionary")
arr = Split("XS,S,M,L,XL,2XL,3XL,4XL", ",")
For i = 0 To UBound(arr)
dic(arr(i)) = i - 20
Next
arr = [a1].CurrentRegion.Offset(1).Resize(, 5).Value
For i = 1 To UBound(arr, 1) - 1
If InStr(arr(i, 2), "A") Then
p = InStr(arr(i, 2), "A")
ElseIf InStr(arr(i, 2), "B") Then
p = InStr(arr(i, 2), "B")
ElseIf InStr(arr(i, 2), "C") Then
p = InStr(arr(i, 2), "C")
End If
If p > 0 Then arr(i, 4) = Mid(arr(i, 2), p + 1) Else arr(i, 4) = 0
If dic.exists(arr(i, 4)) Then
arr(i, 5) = dic(arr(i, 4))
Else
arr(i, 5) = Val(arr(i, 4))
End If
p = 0
Next
Call bsort(arr, 1, UBound(arr, 1) - 1, 1, UBound(arr, 2), 1)
For i = 1 To UBound(arr, 1) - 1
If arr(i, 1) <> arr(i + 1, 1) Then
Call bsort(arr, p + 1, i, 1, UBound(arr, 2), 5)
p = i
End If
Next
[a2].Resize(UBound(arr, 1) - 1, 3) = arr
End Sub
Function bsort(arr, 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 arr(j, key) > arr(j + 1, key) Then
For k = left To right
t = arr(j, k): arr(j, k) = arr(j + 1, k): arr(j + 1, k) = t
Next
End If
Next
Next
End Function |