Sub AssignEmployees()
Dim EmpCol As New Collection
'Dim col_12 As New Collection
Dim MyRand As Integer
Dim Empl As String
Dim count1 As Integer
Dim count2 As Integer
Dim count3 As Integer
Dim r As Range
Dim n As Integer
Dim msg As Integer
Range("D2:H2000").ClearContents
count1 = Range("E1").Value
count2 = Range("G1").Value
count3 = Range("I1").Value
'Add all area 3 employees to collection
Set r = Range("B2")
Do Until r.Value = ""
EmpCol.Add r.Value
Set r = r.Offset(1, 0)
Loop
If EmpCol.Count < count3 Then
msg = MsgBox("You do not have enough employees listed for area 3 to assign the number you want to that area. Add more employees to the list of allowed employees in area 3, or reduce the number in cell I1 you want in that area", vbOKOnly + 64, "")
Exit Sub
End If
'Pick out however many employees is allowed for Area 3(from I1) and randomly put
'in column H and remove that employee from collection
Set r = Range("H2")
Do Until n = count3
Randomize
MyRand = Int((EmpCol.Count * Rnd) + 1)
r.Value = EmpCol(MyRand)
EmpCol.Remove (MyRand)
Set r = r.Offset(1, 0)
n = n + 1
Loop
n = 0
'Add rest of employees to collection from column A
Set r = Range("A2")
Do Until r.Value = ""
EmpCol.Add r.Value
Set r = r.Offset(1, 0)
Loop
If EmpCol.Count < count2 Then
msg = MsgBox("There are not enough employees left over after area 3 to assign the number that you want to area 2. Add more employess to either list, or reduce the number you want in area 2, (cell G1)", vbOKOnly + 64, "")
Exit Sub
End If
'Pick out however many employees is allowed for Area 2(from G1) and randomly put
'in column F and remove that employee from collection
Set r = Range("F2")
Do Until n = count2
Randomize
MyRand = Int((EmpCol.Count * Rnd) + 1)
r.Value = EmpCol(MyRand)
EmpCol.Remove (MyRand)
Set r = r.Offset(1, 0)
n = n + 1
Loop
n = 0
If EmpCol.Count < count1 Then
msg = MsgBox("There are not enough employees left over after area 2 " & _
"and 3 to assign the number that you want to area 1. Add more employess to " & _
"either list, or reduce the number you want in area 1, (cell E1). The employees that" & _
" are left will be listed in area 1 now.", vbOKOnly + 64, "")
End If
'Pick out however many employees is allowed for Area 1(from E1) and randomly put
'in column D and remove that employee from collection
Set r = Range("D2")
On Error GoTo done
Do Until n = count1
Randomize
MyRand = Int((EmpCol.Count * Rnd) + 1)
r.Value = EmpCol(MyRand)
EmpCol.Remove (MyRand)
Set r = r.Offset(1, 0)
n = n + 1
Loop
done:
Exit Sub
End Sub
Sub ClearAssignedAreas()
Range("D2:H2000").ClearContents
End Sub
|