'你这示例是否有问题? 5-7跑哪里去了?
Option Explicit
Sub test()
Dim arr, i, j, m, n, t1, t2
arr = Range("a2:d" & Cells(Rows.Count, "b").End(xlUp).Row + 1)
For i = 1 To UBound(arr, 1) - 1: arr(i, 4) = i: Next
Call dsort(arr, 1, UBound(arr, 1) - 1, 2, True)
For i = 1 To UBound(arr, 1) - 1
For j = i To UBound(arr, 1) - 1
If arr(j, 2) <> arr(j + 1, 2) Then
If j > i Then Call dsort(arr, i, j, 3, True)
i = j: Exit For
End If
Next j, i
n = 1
For i = 1 To UBound(arr, 1) - 1
m = m + 1: arr(i, 1) = "MTC-" & Format(n, "000")
t1 = arr(i, 2) & arr(i, 3)
t2 = arr(i + 1, 2) & arr(i + 1, 3)
If m = 4 Then n = n + 1: m = 0
If t1 <> t2 Then
If m > 0 Then n = n + 1
m = 0
End If
Next
Call dsort(arr, 1, UBound(arr, 1) - 1, 4, True)
[a2].Resize(UBound(arr, 1) - 1, UBound(arr, 2) - 1) = arr
End Sub
Function dsort(arr, first, last, key, order)
Dim i, j, k, t
For i = first To last - 1
For j = i + 1 To last
If arr(i, key) < arr(j, key) Xor order Then
For k = 1 To UBound(arr, 2)
t = arr(i, k): arr(i, k) = arr(j, k): arr(j, k) = t
Next
End If
Next j, i
End Function |