'空为未分配,自己修改
Option Explicit
Sub test()
Dim arr, i, j, k, n, m, t, a, b
arr = Range("a2:c" & Cells(Rows.Count, "a").End(xlUp).Row + 1)
Call dsort(arr, 1, UBound(arr, 1) - 1, 2)
Randomize
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
n = j - i + 1
For a = i To j
m = Int(Rnd * n)
For b = 1 To UBound(arr, 2)
t = arr(a, b): arr(a, b) = arr(i + m, b): arr(i + m, b) = t
Next b, a
For a = i To j - n Mod 4 Step 4
arr(a, 3) = "院一": arr(a + 1, 3) = "院二"
arr(a + 2, 3) = "院三": arr(a + 3, 3) = "院三"
Next
i = j: Exit For
End If
Next j, i
Call dsort(arr, 1, UBound(arr, 1) - 1, 1)
[d2].Resize(UBound(arr, 1), UBound(arr, 2)) = arr
End Sub
Function dsort(arr, first, last, key)
Dim i, j, k, t
For i = first To last - 1
For j = i + 1 To last
If arr(i, key) > arr(j, key) 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 |