|
楼主 |
发表于 2023-12-26 10:38
|
显示全部楼层
Sub lqxs()
Dim Arr, i&, Brr, m%, j%
Dim d As Object, k, dX
Dim xx%, zb1%, zb2%, yx1%, yx2%, ts%, bc, sjs%, sj%
Set d = CreateObject("Scripting.Dictionary")
Set dX = CreateObject("Scripting.Dictionary")
Sheet1.Activate
xx = [b32].Value '同一天休息人数限制
zb1 = [b33].Value: zb2 = [c33].Value '同一天早班人数限制
yx1 = [b34].Value: yx2 = [c34].Value '一个月一个人休息天数限制
ts = [b35].Value '本月天数
bc = [b36].Resize(1, 3) '班次设置
[b20:af29].ClearContents
Randomize
Arr = [a19:a29]
For i = 2 To UBound(Arr)
sjs = Int(Rnd * (yx2 - yx1 + 1)) + yx1 '生成一个月一个人休息天数
Do
sj = Int(Rnd * ts) + 1
If Not d.exists(sj) Then
If d.Count = 0 Then
If dX(sj) < xx Then dX(sj) = dX(sj) + 1 Else GoTo 100
d(sj) = w: k = dkeys
Else
For j = 0 To UBound(k)
If sj <= k(j) + 3 And sj >= k(j) - 3 Then GoTo 100
Next
If dX(sj) < xx Then dX(sj) = dX(sj) + 1 Else GoTo 100
d(sj) = "": k = d.keys
End If
End If
100:
Loop While d.Count < sis
k = d.keys
For j = 0 To UBound(k)
Cells(i + 18, k(j) + 1) = "休"
Next
d.RemoveAll
Next
Arr = [a18].CurrentRegion
For j = 2 To UBound(Arr, 2)
m = 19: [aj:ak].ClearContents
n = Int(Rnd * (zb2 - zb1 + 1)) + zb1 '生成随机早班人数
For i = 3 To UBound(Arr)
If Arr(i, j) = "" Then
m = m + 1: Cells(m, 36) = i: Cells(m, 37) = "=rand()"
End If
Next
[aj20].CurrentRegion.Sort [ak20], 1, Header:=xlNo
Brr = [aj20].Resize(n, 1)
For i = 1 To UBound(Brr)
Cells(Brr(i, 1) + 17, j) = "早"
Next
For i = 3 To UBound(Arr)
If Cells(i + 17, j) = "" Then Cells(i + 17, j) = "晚"
Next
Next
End Sub
|
|