'看了一下相同姓名的所在"工单所在群"可以不同,很难理解,原来代码没有考虑这种可能
'一般来说相同姓名"对应领导"、"工单所在群"应该一样的,唯一可以不同的是"工单",但是事实并不是这样
'换种方法给你重写了一遍,自己测试一下
Option Explicit
Sub test()
Dim arr, t, i, j, k, kk, n, m
arr = Range("a2:d" & Cells(Rows.Count, "a").End(xlUp).Row + 1)
ReDim brr(1 To Rows.Count, 1 To UBound(arr, 2))
t = arr: Call msort(arr, t, 1, UBound(arr, 1) - 1, 1, UBound(arr, 2), 1)
For i = 1 To UBound(arr, 1) - 1
For j = i To UBound(arr, 1) - 1
If arr(j, 1) <> arr(j + 1, 1) Then
If j - i + 1 <= 2 Then '同名<=2人直接获取
For k = i To j
n = n + 1
For kk = 1 To UBound(arr, 2): brr(n, kk) = arr(k, kk): Next
Next
Else
Randomize
For k = i To i + 1 '同名>2人随机取同名中的2人
n = n + 1: m = Int(Rnd * (j - k + 1)) + k
For kk = 1 To UBound(arr, 2)
t = arr(k, kk): arr(k, kk) = arr(m, kk): arr(m, kk) = t
Next
For kk = 1 To UBound(arr, 2): brr(n, kk) = arr(k, kk): Next
Next
End If
i = j: Exit For
End If
Next j, i
With [f2]
.Resize(Rows.Count - 1, UBound(brr, 2)).ClearContents
.Resize(n, UBound(brr, 2)) = brr
End With
End Sub
Function msort(arr, temp, first, last, left, right, key)
Dim i, j, k, kk, mid
If first <> last Then
mid = Int((first + last) / 2)
msort arr, temp, first, mid, left, right, key
msort arr, temp, mid + 1, last, left, right, key
i = first: j = mid + 1: k = first
While i <= mid And j <= last
If arr(i, key) <= 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 |