+ Reply to Thread
Results 1 to 16 of 16

Automatic shift scheduler

Hybrid View

  1. #1
    Registered User
    Join Date
    02-18-2019
    Location
    London, England
    MS-Off Ver
    2019
    Posts
    5

    Automatic shift scheduler

    Hi

    I want to create an automatic shift scheduler in Excel. I've included an example sheet. Basically I would like to create a table each month to schedule shifts for the employees. For each day there is a required number of employees (usually 4 but it can vary) and a maximum number of shifts for each employee per month. Also included in the table are the days certain employees aren't able to work (marked with an "X"). What I would need is Excel to fill out the table based on that criteria. If there are not enough employees with enough shifts to fill out the month, then I'd like it to just leave those cells empty.

    Any help would be appreciated

    Thank you!
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,351

    Re: Automatic shift scheduler

    Welcome to the forum!

    I think you are going to need VBA for this - would you like me to move the thread for you?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    02-18-2019
    Location
    London, England
    MS-Off Ver
    2019
    Posts
    5

    Re: Automatic shift scheduler

    That would be great, thanks!

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,351

    Re: Automatic shift scheduler

    All done.

  5. #5
    Forum Contributor
    Join Date
    08-26-2014
    Location
    Finland
    MS-Off Ver
    365
    Posts
    199

    Re: Automatic shift scheduler

    Ah, this was a fun project

    See my attachment for a practical workbook.

    I really tried to create it to be as flexible as possible. You can add more employees, or days and the code will work.
    You can also manually add workdays, and the code will fill out the rest.

    There are a few things that need to stay the same:

    1. The first employer must be on row 2
    2. No values of the cells that have a Red background can be changed (i search for them in the code, this is what makes the rest so flexible)

    Here is the raw code if someone is interested:

    Option Explicit                                  'Always start your code with this. You have to declare your variables with DIM and REDIM statements, making the code more robust.
    
    'Automatic Shift Scheduler. Coded by: Tuomas "banaanas" Savonius
    
    Sub DoShifts()
        Application.ScreenUpdating = False           'Disable screenupdates for faster code execution
    
        Dim wb         As Workbook                   'ThisWorkbook
        Dim ws         As Worksheet                  'Our Month Worksheet
        Dim c          As Range                      'Helper range
        Dim lastdate   As Range                      'Max times cell
        Dim PeopleNeed As Range                      'People needed cell
        Dim Maxtimes   As Range                      'Max times cell
        Dim i          As Long                       'Helper where we store the amount of people needed that day
        Dim j          As Long                       'Helper where we keep track of the current employer row
        Dim timesAv    As Long                       'Helper where we keep count of the total persons available
    
        'Setting Workbook, worksheet and ranges
        Set wb = ThisWorkbook
        Set ws = wb.ActiveSheet
        Set lastdate = ws.UsedRange.Find(what:="Max times", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
        Set PeopleNeed = ws.UsedRange.Find(what:="People needed", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlNext, MatchCase:=False)
        Set Maxtimes = ws.UsedRange.Find(what:="Times available", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlNext, MatchCase:=False)
    
        'Setting the avaibable resources
        timesAv = ws.UsedRange.Find(what:="Sum of TimesA:", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False).Offset(0, 1).Value
    
    
    
        For Each c In ws.Range(ws.Cells(1, 2), lastdate.Offset(0, -1)) 'Loop all cells from B1 to The lastdate cell - 1 column. This way you can have as many days as you want, and the code will work
            i = ws.Cells(PeopleNeed.Row, c.Column).Value - ws.Cells(PeopleNeed.Row + 1, c.Column) 'Set i to the amoun needed - amount allredy allocated. This will allow you to set "manually" work days in the sheet and code will still work
            j = 2                                    'Set the first row of the employer
            Do While i > 0 And j < PeopleNeed.Row    'Do this loop while we still need people to the day, and our employer row is less then the range where "people needed" is written
                If timesAv > 0 Then                  'if we still have times available then
                    If ws.Cells(j, Maxtimes.Column).Value > 0 And ws.Cells(j, c.Column) = vbNullString Then 'If current employer cell is empty, and the employer still have times avaiable
                        ws.Cells(j, c.Column).Value = "W" 'Write W to cell
                        timesAv = timesAv - 1        'Reduce times avaivable by 1
                        i = i - 1                    'Remove 1 of the days needed work
                        j = j + 1                    'Add to employer row
                    Else
                        j = j + 1                    'If for some reason the above will not work, move to next employer
                    End If
                End If
                If timesAv = 0 Then                  'If there is no available times anymore
                    Exit Do                          'Exit the loop
                End If
            Loop
        Next c                                       'Move to the next day
        Application.ScreenUpdating = True            'Enable screenupdates back on
        MsgBox "Done"
    End Sub
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    02-18-2019
    Location
    London, England
    MS-Off Ver
    2019
    Posts
    5

    Re: Automatic shift scheduler

    Wow! You're absolutely amazing! I probably could have done it with a set table (with a bit of knowledge from Python) but this is flexible as well, which makes it just perfect.

    I feel like you've done so much for me but I have to ask - do you think there could be a way that I could make it fill the days randomly, so people don't get 2 days in a row (if random sticks a person 2 days in a row that would be fine)?

    I can't thank you enough again!

  7. #7
    Registered User
    Join Date
    02-22-2019
    Location
    NY
    MS-Off Ver
    2013
    Posts
    1
    [QUOTE=banaanas;5069337]Ah, this was a fun project

    See my attachment for a practical workbook.

    I really tried to create it to be as flexible as possible. You can add more employees, or days and the code will work.
    You can also manually add workdays, and the code will fill out the rest.

    There are a few things that need to stay the same:

    1. The first employer must be on row 2
    2. No values of the cells that have a Red background can be changed (i search for them in the code, this is what makes the rest so flexible)

    Here is the raw code if someone is interested:

    Option Explicit                                  'Always start your code with this. You have to declare your variables with DIM and REDIM statements, making the code more robust.
    
    'Automatic Shift Scheduler. Coded by: Tuomas "banaanas" Savonius
    
    Sub DoShifts()
        Application.ScreenUpdating = False           'Disable screenupdates for faster code execution
    
        Dim wb         As Workbook                   'ThisWorkbook
        Dim ws         As Worksheet                  'Our Month Worksheet
        Dim c          As Range                      'Helper range
        Dim lastdate   As Range                      'Max times cell
        Dim PeopleNeed As Range                      'People needed cell
        Dim Maxtimes   As Range                      'Max times cell
        Dim i          As Long                       'Helper where we store the amount of people needed that day
        Dim j          As Long                       'Helper where we keep track of the current employer row
        Dim timesAv    As Long                       'Helper where we keep count of the total persons available
    
        'Setting Workbook, worksheet and ranges
        Set wb = ThisWorkbook
        Set ws = wb.ActiveSheet
        Set lastdate = ws.UsedRange.Find(what:="Max times", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
        Set PeopleNeed = ws.UsedRange.Find(what:="People needed", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlNext, MatchCase:=False)
        Set Maxtimes = ws.UsedRange.Find(what:="Times available", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlNext, MatchCase:=False)
    
        'Setting the avaibable resources
        timesAv = ws.UsedRange.Find(what:="Sum of TimesA:", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False).Offset(0, 1).Value
    
    
    
        For Each c In ws.Range(ws.Cells(1, 2), lastdate.Offset(0, -1)) 'Loop all cells from B1 to The lastdate cell - 1 column. This way you can have as many days as you want, and the code will work
            i = ws.Cells(PeopleNeed.Row, c.Column).Value - ws.Cells(PeopleNeed.Row + 1, c.Column) 'Set i to the amoun needed - amount allredy allocated. This will allow you to set "manually" work days in the sheet and code will still work
            j = 2                                    'Set the first row of the employer
            Do While i > 0 And j < PeopleNeed.Row    'Do this loop while we still need people to the day, and our employer row is less then the range where "people needed" is written
                If timesAv > 0 Then                  'if we still have times available then
                    If ws.Cells(j, Maxtimes.Column).Value > 0 And ws.Cells(j, c.Column) = vbNullString Then 'If current employer cell is empty, and the employer still have times avaiable
                        ws.Cells(j, c.Column).Value = "W" 'Write W to cell
                        timesAv = timesAv - 1        'Reduce times avaivable by 1
                        i = i - 1                    'Remove 1 of the days needed work
                        j = j + 1                    'Add to employer row
                    Else
                        j = j + 1                    'If for some reason the above will not work, move to next employer
                    End If
                End If
                If timesAv = 0 Then                  'If there is no available times anymore
                    Exit Do                          'Exit the loop
                End If
            Loop
        Next c                                       'Move to the next day
        Application.ScreenUpdating = True            'Enable screenupdates back on
        MsgBox "Done"
    End Sub
    [/

    Hello. I am unable to see the code. I am trying to make a random schedule for 14 staff members to work a total of 13 shifts a month with all weekends off for everyone. Each employee works 3 shifts a week and one 4 shift week. I need to have 8 people working M,W,Th and F. Tuesday I need 10. Is this possible?

  8. #8
    Forum Contributor
    Join Date
    08-26-2014
    Location
    Finland
    MS-Off Ver
    365
    Posts
    199

    Re: Automatic shift scheduler

    Hello. I am unable to see the code. I am trying to make a random schedule for 14 staff members to work a total of 13 shifts a month with all weekends off for everyone. Each employee works 3 shifts a week and one 4 shift week. I need to have 8 people working M,W,Th and F. Tuesday I need 10. Is this possible?
    I am sure it is, but if you have trouble please open a new thread with your problems.

  9. #9
    Registered User
    Join Date
    02-18-2019
    Location
    London, England
    MS-Off Ver
    2019
    Posts
    5

    Re: Automatic shift scheduler

    My dude, you're absolutely incredible. Thank you so much for helping me out. I can't thank you enough! This is perfect!

    Kiitos paljon!

  10. #10
    Registered User
    Join Date
    01-06-2017
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    3

    Re: Automatic shift scheduler

    Hello. This solution works very well. I was wondering if there was a way to instead of putting a W on the day but to instead put a start and end time of work based on what that employee should be working that day?

  11. #11
    Forum Contributor
    Join Date
    08-26-2014
    Location
    Finland
    MS-Off Ver
    365
    Posts
    199

    Re: Automatic shift scheduler

    Sure I can try to modify it that way.
    Do you wan't it to be fully random, or do you only wan't to prevent that the same person doesn't get 2 days in a row?
    Is there any other limitations that you want to add?

  12. #12
    Registered User
    Join Date
    02-18-2019
    Location
    London, England
    MS-Off Ver
    2019
    Posts
    5

    Re: Automatic shift scheduler

    Fully random would be great. I think that would just put the icing on the cake.

  13. #13
    Forum Contributor
    Join Date
    08-26-2014
    Location
    Finland
    MS-Off Ver
    365
    Posts
    199

    Re: Automatic shift scheduler

    Alright see attachment for an entirely random solution.

    It got a bit messy, but the basic principle is this:
    Store all days in a collection. Create an array with randomised numbers (1 to count of days) use a loop to select dates randomly using this array's values.

    Option Explicit                                  'Always start your code with this. You have to declare your variables with DIM and REDIM statements, making the code more robust.
    
    'Automatic Shift Scheduler. Coded by: Tuomas "banaanas" Savonius
    'User needs to input variables on worksheet for the code to work properly!
    
    Sub DoShifts()
        Application.ScreenUpdating = False           'Disable screenupdates for faster code execution
    
        Dim wb         As Workbook                   'ThisWorkbook
        Dim ws         As Worksheet                  'Our Month Worksheet
        Dim c          As Range                      'Helper range
        Dim lastdate   As Range                      'Max times cell
        Dim PeopleNeed As Range                      'People needed cell
        Dim Maxtimes   As Range                      'Max times cell
        Dim i          As Long                       'Helper where we store the amount of people needed that day
        Dim j          As Long                       'Helper where we keep track of the current employer row
        Dim timesAv    As Long                       'Helper where we keep count of the total persons avaivable
        Dim cCol       As New Collection             'Collection where we will store cell dates
        Dim helpArray() As Long                      'Helper array to loop collection
        Dim k          As Long                       'Helper to loop collection
        Dim l          As Long                       'Helper to loop collection
    
        'Setting Workbook, worksheet and ranges
        Set wb = ThisWorkbook
        Set ws = wb.ActiveSheet
        Set lastdate = ws.UsedRange.Find(what:="Max times", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
        Set PeopleNeed = ws.UsedRange.Find(what:="People needed", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlNext, MatchCase:=False)
        Set Maxtimes = ws.UsedRange.Find(what:="Times available", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlNext, MatchCase:=False)
    
        'Setting the avaibable resources
        timesAv = ws.UsedRange.Find(what:="Sum of TimesA:", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False).Offset(0, 1).Value
    
        For Each c In ws.Range(ws.Cells(1, 2), lastdate.Offset(0, -1)) 'Loop all cells from B1 to The lastdate cell - 1 column. This way you can have as many days as you want, and the code will work
            cCol.Add c                               'Add range to collection
        Next c                                       'Move to the next day
        
        helpArray() = randomArr(cCol.Count)          'Create random order array
        ReDim Preserve helpArray(1 To cCol.Count)    'Make array as long as the collection
        l = 1                                        'set l to first position
        
        For k = 1 To cCol.Count                      'Loop all collections
            Set c = cCol(helpArray(l))               'select a random cell from collection
            i = ws.Cells(PeopleNeed.Row, c.Column).Value - ws.Cells(PeopleNeed.Row + 1, c.Column) 'Set i to the amoun needed - amount allredy allocated. This will allow you to set "manually" work days in the sheet and code will still work
            j = 2                                    'Set the first row of the employer
            Do While i > 0 And j < PeopleNeed.Row    'Do this loop while we still need people to the day, and our employer row is less than the range where "people needed" is written
                If timesAv > 0 Then                  'if we still have times avaivable then
                    If ws.Cells(j, Maxtimes.Column).Value > 0 And ws.Cells(j, c.Column) = vbNullString Then 'If current employer cell is empty, and the employer still have times avaiable
                        ws.Cells(j, c.Column).Value = "W" 'Write W to cell
                        timesAv = timesAv - 1        'Reduce times avaivable by 1
                        i = i - 1                    'Remove 1 of the days needed work
                        j = j + 1                    'Add to employer row
                    Else
                        j = j + 1                    'If for some reason the above will not work, move to next employer
                    End If
                End If
                If timesAv = 0 Then                  'If there is no avaivable times anymore
                    Exit Do                          'Exit the loop
                End If
            Loop
            l = l + 1                                'increase to next array item
        Next
        
        Application.ScreenUpdating = True            'Enable screenupdates back on
        MsgBox "Done"
    End Sub
    
    'Function to generate no duplicate values into an array. Base done by aevenko on stackoverflow
    
    Function randomArr(maxnum As Long)
    
        Dim i          As Long, n As Long
        Dim numArray(1 To 1000) As Long
        Dim numCollection As New Collection
    
        With numCollection
            For i = 1 To maxnum
                .Add i
            Next
            For i = 1 To maxnum
                n = Rnd * (.Count - 1) + 1
                numArray(i) = numCollection(n)
                .Remove n
            Next
        End With
    
        randomArr = numArray()
    
    End Function
    Attached Files Attached Files

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,351

    Re: Automatic shift scheduler

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

  15. #15
    Registered User
    Join Date
    04-28-2021
    Location
    Austin, TX
    MS-Off Ver
    365
    Posts
    1

    Re: Automatic shift scheduler

    Hi Bannanas:

    I'm looking for something similar to Generate a the Schedule for our Church, Basically we have 30+ Volunteers that serve on Average 4 Services a Week, however, the biggest Issues is they all have different availability as to the days they can serve so it would be great for the Shift Scheduler to consider rules as to what days of the week they can serve.

  16. #16
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,047

    Re: Automatic shift scheduler

    Quote Originally Posted by ricbrear View Post
    Hi Bannanas:

    I'm looking for something similar to Generate a the Schedule for our Church, Basically we have 30+ Volunteers that serve on Average 4 Services a Week, however, the biggest Issues is they all have different availability as to the days they can serve so it would be great for the Shift Scheduler to consider rules as to what days of the week they can serve.
    Did you perhaps miss the post IMMEDITELY above yours?

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. 3 Questions on the "Employee Shift Scheduler" Template
    By Pai Mei in forum Excel General
    Replies: 3
    Last Post: 09-20-2017, 07:32 PM
  2. Shift Scheduler problem
    By vagg3lis in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-10-2015, 05:36 PM
  3. Replies: 0
    Last Post: 10-27-2014, 11:54 PM
  4. Running Excel from task scheduler. Scheduler doesn't end
    By tony h in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-31-2013, 09:49 PM
  5. Automatic Scheduler
    By Nuggetross in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-24-2013, 12:23 PM
  6. Want to make automatic scheduler
    By dragonlor20 in forum Excel General
    Replies: 0
    Last Post: 05-20-2012, 10:46 PM
  7. Shift cell focus automatic
    By anurag.agarwal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2010, 01:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1