Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Employee Monthly Schedule

0

Hi ..

I have an employee schedule that contains 3 shifts covering 24 hours. I want the work schedule to be the same without change for two weeks, while maintaining the number of employees in each shift at no less than 7 daily throughout the month, and the work schedule is 6 days, with the seventh day off.

Code_Goes_Here

Sub AssignShifts()

    Dim ws As Worksheet

    Dim employees As Range

    Dim days As Integer, i As Integer, j As Integer

    Dim shifts(3) As String

    Dim colors(3) As Long

    Dim empShifts(28, 31) As String

    Set ws = ActiveSheet

    Set rng = ws.Range("Table")

    ' Define shift symbols and colors

    shifts(1) = "D": colors(1) = RGB(0, 0, 255)   ' Blue for Day

    shifts(2) = "E": colors(2) = RGB(0, 128, 0)   ' Green for Evening

    shifts(3) = "N": colors(3) = RGB(255, 0, 0)     ' Red for Night

    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name

    Set employees = ws.Range("B12:B39") ' 28 employees listed from D12 to D39

    days = 30 ' Number of days in the month

    ' Clear previous assignments

    ws.Range("D12:AH39").ClearContents

    ws.Range("D12:AH39").Interior.ColorIndex = xlNone

    ' Fixed two-week pattern per employee group

    ' Divide 28 employees into 3 groups for rotation (approx. 9-10 employees per group)

    Dim shiftGroup As Integer

    Dim empStartCol As Integer

    Dim empShift As String

    For i = 1 To 28

        shiftGroup = ((i - 1) Mod 3) + 1 ' Cycle groups 1, 2, 3 for D, E, N

        For j = 1 To days

            empStartCol = j + 4 ' Adjust column (E is column 5)

            ' Determine 2-week cycle shift

            Select Case ((Int((j - 1) / 14) + shiftGroup - 1) Mod 3) + 1

                Case 1: empShift = "D"

                Case 2: empShift = "E"

                Case 3: empShift = "N"

            End Select

            ' Assign OFF on every 7th day of the cycle (6 work days, then OFF)

            If (j Mod 7 = 0) Then

                ws.Cells(i + 11, empStartCol).Value = "OFF"

                ws.Cells(i + 11, empStartCol).Interior.ColorIndex = xlNone

            Else

                ws.Cells(i + 11, empStartCol).Value = empShift

                Select Case empShift

                    Case "D": ws.Cells(i + 11, empStartCol).Interior.Color = colors(1)

                    Case "E": ws.Cells(i + 11, empStartCol).Interior.Color = colors(2)

                    Case "N": ws.Cells(i + 11, empStartCol).Interior.Color = colors(3)

                End Select

            End If

        Next j

    Next i

    MsgBox "Fixed 2-week rotating shift schedule completed!", vbInformation

End Sub

Answer
Discuss

Discussion

Hello Nurse911 and welcome to the forum,

First, when posting code, after clicking "CODE" you then remove/delete the text "Code_Goes_Here" and paste in your code. It makes it easier to read and to copy. 
Second, if you could upload a sample file it would greatly help to understand the structure of your file without trying to recreate it. 
In the code you posted, the line "Set employees = ws.Range("B12:B39") ' 28 employees listed from D12 to D39" is a bit confusing. Is the reference "D12 to D39" correct or should it be "B12 to B39" ? Will the employees rotate through the three shifts or will they always stay on the same shift?

If you could upload a sample file that will go a long way to helping us to help you. (edit your original post and use one of the links at the bottom of your post)

Cheers   :-)
WillieD24 (rep: 677) Apr 4, '25 at 11:16 pm
Hi Nurse911,

I logged in to see if you had added a sample file. I got a notification that the post had been updated but I don't notice any changes and there is no sample file.
WillieD24 (rep: 677) Apr 5, '25 at 11:57 am
@WillieD24 - I looked that this subject earlier and was pretty sure I saw a file attached to the nurse911's Question (but thought to leave it to you to answer so didn't open it).

Perhaps it was removed since it included personal daya (which we don't want to see). Hopefully you'll get a revised file with "dunmy" data.
John_Ru (rep: 6652) Apr 5, '25 at 4:17 pm
@nurse911
Have you figured it out on your own? If so, please mention it in the comments. If you still need help, first we need you to help us. Please upload a sample file (with no personal info). The sample file should include a worksheet showing the layout you intend to use. If there are 28 employees split into 3 shifts over 24 hours there will be three crews: 2 of 9 employees and 1 of 10 employees. Will all employees be working all days (6 per week)? Will the employees be always working the same shift or will the employees be rotating through the shifts?

Hope to hear from you soon.
WillieD24 (rep: 677) Apr 6, '25 at 7:47 pm
Add to Discussion

Answers

0

Hello Nurse911,

While waiting for you to reply I have playing around with this. There are a few things about your code that need fixing.

1)    You have the variable "Rng" in your code but is not defined and not used.

2)     There are 3 shifts so the schedule should be a six week (42 day) repeating schedule. As a 28 day schedule, employee1 will work 2 weeks "D", 2 weeks "E", but never work "N".

3)     Variable "days" is set to 30 but is not used in the code

4)     Adjusted schedule columns from "D" thru "AH" to "E" thru "AT" (6 weeks instead of 30 days)

5)     Changed fill colours to lighter shades so text is easier to read

The modifications are found in Module2 ( Sub AssignShifts_2() )

I also added a button to Sheet1 to run the macro. See attached file.

Hope this helps; please advise. If this solves things please mark my answer as Selected.

Cheers   :-)

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login