+ Reply to Thread
Results 1 to 10 of 10

VBA Schedule help!

  1. #1
    Registered User
    Join Date
    07-17-2017
    Location
    Ohio, United States
    MS-Off Ver
    2010
    Posts
    6

    VBA Schedule help!

    I am very new to Excel Macros so naturally I am stuck on a problem. I'm working on a scheduling tool. The tool would take a schedule i.e. 6 days on, 3 days off, and pair it with a worker. However the repeating schedule starts on a different day for each employee. For example. Employee A started his 6 day on, 3 day off schedule on 7/7/2017 and Employee B started hers on 7/10/2017.

    I have my spreadsheet set up with dates in the top header row with a column of all employees. I would like to make an if statement that says "if the employees start date matches the date in the header row, then insert a repeating 6 day on 3 day off schedule."

    I'm assuming I will need to use VBA.

    Any help would be greatly appreciated!!

    Thank you,

    NZ

  2. #2
    Registered User
    Join Date
    04-12-2011
    Location
    Bay Lake, FL
    MS-Off Ver
    Excel 2016 / 365
    Posts
    66

    Re: VBA Schedule help!

    Try this.

    In the spirit of education, what the macro is doing is basically running through 2 loops.

    First, we find the last used row in Column A - this is where the employee names are. This allows the script to be adaptable to any number of employees.

    It will loop through each row one at a time, and find the start date, from Column B.

    Then, a second loop will check each column to see if the date at the top of that column (Row 5) matches the employees "start date". If it does, it populates that employees row/that date with "On", and fills in 5 more "On" and 3 "Off", then repeats.

    After it's done with that row, it'll move on to the next row, until we reach the last row.
    Attached Files Attached Files

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: VBA Schedule help!

    This may or may not need VBA. It potentially could be solved using the offset command and/or an array formula. We need to see how you have things organized to judge better.

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  4. #4
    Registered User
    Join Date
    07-17-2017
    Location
    Ohio, United States
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA Schedule help!

    attached is a sample. There are 3 sheets. Before, after, and schedules. The schedules tab is what I would like to reference in my formula/vba. Thanks for all your help!!
    Attached Files Attached Files

  5. #5
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: VBA Schedule help!

    I was wrong, it did not take the offset command nor an array formula. This can be done with MOD.

    I had a couple intermediate formulas and I substituted back until I had the formula in terms of the two helper columns I inserted. I could substitute further back, but that would make the final formula even more complicated.

    The two helper columns are Columns D & E and they have the following formulas: =1*LEFT(C2,1) and =1*RIGHT(C2,1) respectively. The reason for multiplying by 1 is to force the string into a numerical value.

    Then the magical formula is: =IF(MATCH($B2,$1:$1,0)>COLUMN(),"",IF(MOD(COLUMN()-MATCH($B2,$1:$1,0),$D2+$E2)<$D2,"On Duty",""))

    The first part is MATCH($B2,$1:$1,0)>COLUMN() if this is true it means the column is before the start date so display a blank cell.

    This part gets the column number for the start date: MATCH($B2,$1:$1,0)

    So COLUMN()-MATCH($B2,$1:$1,0) "baselines" the start date column to zero, subsequent columns equate to 1, 2, ...

    MOD(COLUMN()-MATCH($B2,$1:$1,0),$D2+$E2)<$D2 works the magic.

    Let's take a 2/1 - two on, one off as an example. And furthermore let's say the person starts on June 2.

    So Column H, June 2 is 0, June 3 is 1, June 4 is 2, June 5 is 3, June 6 is 4.


    June 2 is MOD (0, 2+1) or 0
    June 3 is MOD (1, 2+1) or 1
    June 4 is MOD (2, 2+1) or 2
    June 5 is MOD (3, 2+1) which brings us back to zero.
    June 6 is MOD (4, 2+1) or 1

    Now we compare this to the ON days or 2. If the MOD yields less than 2 then the person is ON DUTY. Otherwise display a blank cell.
    Attached Files Attached Files

  6. #6
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: VBA Schedule help!

    P.S. I put the formulas on the Employee Before sheet. I tested out several combinations like 5/5, 1/2, 2/1 and 3/2 - it seems to work across the board.

  7. #7
    Registered User
    Join Date
    07-17-2017
    Location
    Ohio, United States
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA Schedule help!

    Wow! Thank you so much for all of your help!

  8. #8
    Registered User
    Join Date
    07-17-2017
    Location
    Ohio, United States
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA Schedule help!

    This is great! Thank you for your help!

  9. #9
    Registered User
    Join Date
    07-17-2017
    Location
    Ohio, United States
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA Schedule help!

    One final question. Suppose I had a 5 on 2 off 5 on 3 off schedule. (5/2/5/3) what would be needed to alter the format of the sheet to accommodate this?

  10. #10
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: VBA Schedule help!

    That would be a lot more complicated. It probably could be done with formulas, but might have crossed the line to where a VB solution would be easier. I'll look at it if I have time.

+ 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. Tiered Interest Schedule and Loan Amortization Schedule
    By wh1968mbz in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-28-2015, 07:43 AM
  2. Automate Monthly schedule to Daily Schedule
    By Frytoos in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-03-2014, 09:09 AM
  3. Create Individual Schedule from Master Schedule
    By kscheller in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-24-2014, 11:47 PM
  4. Replies: 8
    Last Post: 07-15-2013, 07:13 PM
  5. Updating a Schedule based on a master schedule
    By Lukerich in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-21-2013, 12:18 PM
  6. create daily schedule using master task schedule
    By DCO in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-13-2012, 01:08 PM
  7. Replies: 8
    Last Post: 10-12-2005, 12:05 AM

Tags for this Thread

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