Creating a randomly populated roster from a list

Pete067

New Member
Joined
May 25, 2019
Messages
4
Hi all,
I am a new user to Excel and would like to create a roster for work. I am a teacher and I need to allocate staff to Areas around the school for supervision. I have attached the roster outline with some random names and criteria for the roster. I feel that with some guidance in the right direction I could complete this roster. Ideally I would like a button that I can press that self generates this roster.
https://drive.google.com/file/d/1gnJsllLIyGpNh5tqyKZL6--mfZHe8Azm/view?usp=sharing
Thank you
Pete
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi Pete,

Welcome to the forum, and to Excel! I'm going to give this a whirl, but need clarification on one of the conditions:

*Only allocate Doug, Mike, Wendy and Mark to Area 16 and 17

Do you exclusively want those staff members assigned to area 16 and 17 (nobody else can be assigned to 16 and 17)

or

Is it that Doug, Mike, Wendy, and Mark are only allowed to be assigned to those areas (no other areas are allowed for those staff members)


All the best,
Matt
 
Upvote 0
Hi Pete,

Welcome to the forum, and to Excel! I'm going to give this a whirl, but need clarification on one of the conditions:

*Only allocate Doug, Mike, Wendy and Mark to Area 16 and 17

Do you exclusively want those staff members assigned to area 16 and 17 (nobody else can be assigned to 16 and 17)

or

Is it that Doug, Mike, Wendy, and Mark are only allowed to be assigned to those areas (no other areas are allowed for those staff members)


All the best,
Matt


Hi Matt,

Thanks for helping me out!
Doug can only do 16 and 17. Mike, Wendy and Mark are the only staff that assist Doug in this Area, but can be also allocated to other Areas. In addition I will now need to close Area 4 on Monday and Tuesday.

Many thanks
Pete
 
Upvote 0
Pete,

Although my method is probably laughable, I think it produces the results you want:

**Please try this on a copy of your worksheet and test extensively before running it on your original sheet - Always save your work before running the macro**

First, I set up a "helper" sheet called "Sheet2". If you already have a Sheet2, it will have to be called something else, and the code will have to be fixed accordingly. The sheet looks like this, and will have to be set up exactly like this for the code to work:

Excel 2007 32 bit
ABCDEFGHIJKLMNOPQRSTUVW
1StaffMondayTuesdayWednesdayThursdayFridayArea 1Area 2Area 3Area 4Area 5Area 6Area 7Area 8Area 9Area 10Area 11Area 12Area 13Area 14Area 15Area 16Area 17
2MaryXXXX
3JohnXX
4JosephXX
5MargXX
6JennyXXXXX
7MarieXX
8MattXX
9NoahXXXXXX
10JimXX
11BenXX
12SallyXX
13AmyXX
14LarryXX
15FredXXXX
16Wendy
17Mark
18Mike
19DougXXXXXXXXXXXXXXX

<tbody>
</tbody>
Sheet2


Column A lists all of the staff members. Row 1 lists conditions where staff members cannot work. Each condition should be filled in with an "X" in the appropriate cell.

Next, open your VBA editor by pressing Alt + F11 and paste the following code into a module:
(This is the laughable part - I'm sure someone else could have made this much cleaner :rolleyes:)

Code:
Sub randomize_roster()
Dim randnum As Integer, staff As String, x As String, y As String, lastrow As Integer, areaval As Integer, dayval As Integer, ival As Integer, check As Integer, timeslooped As Integer, areacheck As Integer
Application.ScreenUpdating = False

Range("B3:F21").ClearContents

tryagain:
timeslooped = 0
dayval = 2
areaval = 22

lastrow = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Activate

area16:
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area16
Else
Worksheets("Sheet1").Cells(20, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea16
Else: GoTo area16
End If
End If

endarea16:
dayval = 2
areaval = 23

area17:
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area17
Else
check = 20
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
GoTo area17
Else
End If
Worksheets("Sheet1").Cells(21, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea17
Else: GoTo area17
End If
End If

endarea17:
dayval = 2
areaval = 7


area1:
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
Sheets("Sheet2").Activate
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area1
Else
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area1
Else
End If
Next check

Select Case dayval
Case 1 To 2
Case 3
If Worksheets("Sheet1").Cells(3, 2).Value = staff Then
timeslooped = timeslooped + 1
GoTo area1
End If
Case 4
For areacheck = 2 To 3
If Worksheets("Sheet1").Cells(3, areacheck).Value = staff Then
timeslooped = timeslooped + 1
GoTo area1
End If
Next areacheck
Case 5
For areacheck = 2 To 4
If Worksheets("Sheet1").Cells(3, areacheck).Value = staff Then
timeslooped = timeslooped + 1
GoTo area1
End If
Next areacheck
Case 6
For areacheck = 2 To 5
If Worksheets("Sheet1").Cells(3, areacheck).Value = staff Then
timeslooped = timeslooped + 1
GoTo area1
End If
Next areacheck
End Select

Worksheets("Sheet1").Cells(3, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea1
Else: GoTo area1
End If
End If

endarea1:
dayval = 2
areaval = 8

area2:
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area2
Else
check = 3
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area2
Else
End If
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area2
Else
End If
Next check

Select Case dayval
Case 1 To 2
Case 3
If Worksheets("Sheet1").Cells(4, 2).Value = staff Then
timeslooped = timeslooped + 1
GoTo area2
End If
Case 4
For areacheck = 2 To 3
If Worksheets("Sheet1").Cells(4, areacheck).Value = staff Then
timeslooped = timeslooped + 1
GoTo area2
End If
Next areacheck
Case 5
For areacheck = 2 To 4
If Worksheets("Sheet1").Cells(4, areacheck).Value = staff Then
timeslooped = timeslooped + 1
GoTo area2
End If
Next areacheck
Case 6
For areacheck = 2 To 5
If Worksheets("Sheet1").Cells(4, areacheck).Value = staff Then
timeslooped = timeslooped + 1
GoTo area2
End If
Next areacheck
End Select








Worksheets("Sheet1").Cells(4, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea2
Else: GoTo area2
End If
End If

endarea2:
dayval = 2
areaval = 9

area3:
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area3
Else
For check = 3 To 4
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area3
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area3
Else
End If
Next check
Worksheets("Sheet1").Cells(6, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea3
Else: GoTo area3
End If
End If

endarea3:
dayval = 4
areaval = 10

area4:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area4
Else
For check = 3 To 6
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area4
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area4
Else
End If
Next check
Worksheets("Sheet1").Cells(7, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea4
Else: GoTo area4
End If
End If

endarea4:

dayval = 2
areaval = 11

area5:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area5
Else
For check = 3 To 7
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area5
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area5
Else
End If
Next check
Worksheets("Sheet1").Cells(8, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea5
Else: GoTo area5
End If
End If

endarea5:
dayval = 2
areaval = 12

area6:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area6
Else
For check = 3 To 8
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area6
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area6
Else
End If
Next check
Worksheets("Sheet1").Cells(9, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea6
Else: GoTo area6
End If
End If

endarea6:
dayval = 2
areaval = 13

area7:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area7
Else
For check = 3 To 9
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area7
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area7
Else
End If
Next check
Worksheets("Sheet1").Cells(10, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea7
Else: GoTo area7
End If
End If

endarea7:
dayval = 2
areaval = 14

area8:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area8
Else
For check = 3 To 10
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area8
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area8
Else
End If
Next check
Worksheets("Sheet1").Cells(11, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea8
Else: GoTo area8
End If
End If

endarea8:
dayval = 2
areaval = 15

area9:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area9
Else
For check = 3 To 11
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area9
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area9
Else
End If
Next check
Worksheets("Sheet1").Cells(13, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea9
Else: GoTo area9
End If
End If

endarea9:
dayval = 2
areaval = 16

area10:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area10
Else
For check = 3 To 13
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area10
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area10
Else
End If
Next check
Worksheets("Sheet1").Cells(14, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea10
Else: GoTo area10
End If
End If

endarea10:
dayval = 2
areaval = 17

area11:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area11
Else
For check = 3 To 14
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area11
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area11
Else
End If
Next check
Worksheets("Sheet1").Cells(15, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea11
Else: GoTo area11
End If
End If

endarea11:
dayval = 2
areaval = 18

area12:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area12
Else
For check = 3 To 15
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area12
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area12
Else
End If
Next check
Worksheets("Sheet1").Cells(16, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea12
Else: GoTo area12
End If
End If

endarea12:
dayval = 2
areaval = 19

area13:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area13
Else
For check = 3 To 16
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area13
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area13
Else
End If
Next check
Worksheets("Sheet1").Cells(17, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea13
Else: GoTo area13
End If
End If

endarea13:
dayval = 2
areaval = 20

area14:
If timeslooped > 12500 Then GoTo tryagain Else
randnum = Int((lastrow - 2 + 1) * Rnd + 2)
staff = Worksheets("Sheet2").Cells(randnum, 1).Value
x = WorksheetFunction.Index(ActiveWorkbook.Worksheets("Sheet2").Range(Cells(1, dayval), ActiveWorkbook.Worksheets("Sheet2").Cells(lastrow, dayval)), randnum, 0)
y = WorksheetFunction.Index(ActiveWorkbook.Sheets("Sheet2").Range(Cells(1, areaval), Worksheets("Sheet2").Cells(lastrow, areaval)), randnum, 0)
If x = "X" Or y = "X" Then
GoTo area14
Else
For check = 3 To 17
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area14
Else
End If
Next check
For check = 20 To 21
If Worksheets("Sheet1").Cells(check, dayval).Value = staff Then
timeslooped = timeslooped + 1
GoTo area14
Else
End If
Next check
Worksheets("Sheet1").Cells(18, dayval).Value = staff
dayval = dayval + 1
If dayval = 7 Then
GoTo endarea14
Else: GoTo area14
End If
End If

endarea14:


For Z = 2 To lastrow
staff = Cells(Z, 1).Value
ival = Application.WorksheetFunction.CountIf(Sheets("Sheet1").Range("B3:F21"), staff)
If ival < 1 Then
    dayval = 2
    areaval = 7
    GoTo area1
Else
End If
Next Z

Sheets("Sheet1").Activate
Application.ScreenUpdating = True
End Sub

Near the very end of the code (starting with "For Z=2 to lastrow" and ending with "Next Z") ensures that everyone gets on the schedule at least once. This can be taken out if that isn't necessary. (But it helps make sure that Noah doesn't miss out on his day.

Lastly, I put the following formula in cell J3 (next to Mary's name):

Code:
=COUNTIF($B$3:$F$21,I3)

Copy this down to the last staff member. This will tell you how many times they are on the schedule that week.

The result will give you something like this:

Excel 2007 32 bit
ABCDEFGHIJ
1Duty Roster
2MondayTuesdayWednesdayThursdayFridayStaff
3Area 1JosephJimAmyBenMargMary5
4Area 2MarieMattSallyLarryJennyJohn5
5Joseph5
6Area 3MattJosephMattJimJimMarg5
7Area 4MargAmyAmyJenny2
8Area 5MargLarryMarieFredJohnMarie5
9Area 6FredAmyJimMattLarryMatt5
10Area 7BenFredLarryMaryFredNoah1
11Area 8MaryMargFredMarkMattJim5
12Ben5
13Area 9LarrySallyJohnJennyMarieSally5
14Area 10JohnJohnJosephSallyBenAmy5
15Area 11JimMarieMikeMarieMaryLarry5
16Area 12AmyBenMaryJohnNoahFred5
17Area 13MarkMaryBenMargSallyWendy5
18Area 14SallyMikeWendyJosephJosephMark3
19Mike5
20Area 16WendyDougMarkMikeWendyDoug2
21Area 17MikeWendyDougWendyMike

<tbody>
</tbody>
Sheet1

Adding areas or changing the set-up of Sheet1 will cause errors in the code. Adding staff members, and changing around their work schedule should be fine though.

This should be as random as it gets, but certain staff members will pop up more frequently in a certain area depending on their availability. I should also mention that because of the size of the code, it may take a while to complete depending on computer speed. It took anywhere from about 2 - 30 seconds for me.

I hope this helps!


All the best,
Matt
 
Upvote 0
Thanks Matt!
I am grateful for your time and effort.
I haven't yet succeeded in running the code. My Mac keeps freezing when I do. I will try on a MS Windows computer as soon as I can and let you know if there is any difference.
Thanks again
Pete
 
Upvote 0
Pete,

I’ve never used Excel for Mac, but from what I understand, there are some significant differences in the two versions, and that would effect the VBA needed. It works for me on a Windows computer, so please let me know how it goes.

Matt
 
Upvote 0
Thanks, that's good to know. I thought I was doing something wrong. I'll let you know when I've tested it.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top