|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
逻辑:
1.建立排班人的名字和上班人的字典
2.随机生成上班人的名字,数字代替。
3.设置两个如果,满足最少上班天数。
注意:这里有个GOTO,设置排班天数超过7天,会死循环。
Sub 随机排班()
Set dic_name = CreateObject("scripting.dictionary")
Set dic_on_work = CreateObject("scripting.dictionary")
With Worksheets("sheet1")
.Rows("4:8").ClearContents
arr = .[a3].Resize(1, 15).Value
For i = 1 To UBound(arr, 2)
dic_name(i) = 0
Next
Randomize
max_num = UBound(arr, 2) + 1
min_num = 1
For days_ = 1 To 5 '排班日子
For people = 1 To 5 '一天安排多少人上班
重做:
on_work = Int((max_num - min_num) * Rnd + min_num)
If Not dic_on_work.Exists(on_work) And dic_name(on_work) < 1 Then
dic_name(on_work) = dic_name(on_work) + 1
.Cells(days_ + 3, on_work) = "√"
dic_on_work(on_work) = 0
ElseIf Not dic_on_work.Exists(on_work) And dic_name(on_work) < 2 Then
dic_name(on_work) = dic_name(on_work) + 1
.Cells(days_ + 3, on_work) = "√"
dic_on_work(on_work) = 0
Else
GoTo 重做 '排班日子的不要设置太大,不然会死循环。
End If
Next
dic_on_work.RemoveAll
Next
End With
End Sub
|
|