+ Reply to Thread
Results 1 to 12 of 12

Calculating Due-date and time, Workdays, with specific work hours?

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2016
    Location
    CENTRAL MARMOTTE
    MS-Off Ver
    2010
    Posts
    8

    Calculating Due-date and time, Workdays, with specific work hours?

    I need to calculate a Duedate and time based on Production Hours, on workdays and specific work hours.

    Basically:
    Start Date/Time: (or actually ending time order before this one)
    2016-11-15 09:00 (First entered manual)

    Amount of hours for production: (entered manually)
    15

    Workday start (needs to be flexible):
    07:00

    Workday end (needs to be flexible):
    16:00

    And I need to get out:
    Finished Date/Time:
    [Automatically Calculated]



    If it is possible it would also be great if it was possible to just enter the next cell underneith with the same formula to get the next product in line calculated etc.


    Any ideas?



    /Jocce

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

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    Been there, done that. Here is a module that contains a function called WorkHours. You can use this as a formula in a cell.

    WorkHours(StartDate As Date, NumHours As Double, StartShift As Date, EndShift As Date, Optional Holidays As Range)
    StartDate is the date and time of the start of the project.
    NumHours is the number of hours estimated for the project
    StartShift is the time of day for shift start
    EndShift is the time of day for shift end.
    Holidays is an optional range containing the dates of holidays.

    The result is a date and time when the project will be completed and this should be within working hours.

    Function WorkHours(StartDate As Date, NumHours As Double, StartShift As Date, EndShift As Date, _
        Optional Holidays As Range) As Date
    Dim StartDay As Date            ' Start of the workday
    Dim EndDay As Date              ' End of the workday
    Dim EndTime As Date             ' Ending time of project
    Dim EndDate As Date             ' Ending date of project
    
    ' Intitalize variables
    EndTime = StartDate + NumHours / 24
    EndDate = Int(StartDate)
    StartDay = EndDate + StartShift
    EndDay = EndDate + EndShift
    
    ' Calculate whole days
    While EndTime > EndDay ' while the end time exceeds end of current day
        
        If IsWorkday(StartDay, Holidays) = False Then
        ' If it is a weekday or holiday, push the days and due day up by a day and do nothing else
            StartDay = StartDay + 1
            EndDay = EndDay + 1
            EndTime = EndTime + 1
        Else
        ' If it is a workday, push the day up, but keep due the same
            EndDay = EndDay + 1
            If NumHours > 24 * (EndShift - StartShift) Then
            ' If you need more hours than is in a day, decrement the hours and push up the start date
                NumHours = NumHours - 24 * (EndShift - StartShift)
                StartDay = StartDay + 1
            End If
            If IsWorkday(StartDay, Holidays) = False And NumHours > (EndShift - StartShift) Then
               EndTime = EndTime + 1
            End If
        End If
    Wend
    
    WorkHours = StartDay + NumHours / 24
    
    End Function
    
    Function IsWorkday(MyDate As Date, Optional Holidays As Range) As Boolean
    Dim cl As Range
    Dim bRtn As Boolean
    Dim WkDay As Long
    
    bRtn = True
    
    If Not Holidays Is Nothing Then
        For Each cl In Holidays
            If Int(MyDate) = cl.Value Then
                bRtn = False
                Exit For
         End If
        Next
    End If
    
    WkDay = Weekday(MyDate, vbMonday)
    
    If WkDay > 5 Then
        bRtn = False
    End If
    
    IsWorkday = bRtn
           
    End Function
    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.

  3. #3
    Registered User
    Join Date
    11-15-2016
    Location
    CENTRAL MARMOTTE
    MS-Off Ver
    2010
    Posts
    8

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    Quote Originally Posted by dflak View Post
    Been there, done that. Here is a module that contains a function called WorkHours. You can use this as a formula in a cell.

    WorkHours(StartDate As Date, NumHours As Double, StartShift As Date, EndShift As Date, Optional Holidays As Range)
    StartDate is the date and time of the start of the project.
    NumHours is the number of hours estimated for the project
    StartShift is the time of day for shift start
    EndShift is the time of day for shift end.
    Holidays is an optional range containing the dates of holidays.

    The result is a date and time when the project will be completed and this should be within working hours.

    Function WorkHours(StartDate As Date, NumHours As Double, StartShift As Date, EndShift As Date, _
        Optional Holidays As Range) As Date
    Dim StartDay As Date            ' Start of the workday
    Dim EndDay As Date              ' End of the workday
    Dim EndTime As Date             ' Ending time of project
    Dim EndDate As Date             ' Ending date of project
    
    ' Intitalize variables
    EndTime = StartDate + NumHours / 24
    EndDate = Int(StartDate)
    StartDay = EndDate + StartShift
    EndDay = EndDate + EndShift
    
    ' Calculate whole days
    While EndTime > EndDay ' while the end time exceeds end of current day
        
        If IsWorkday(StartDay, Holidays) = False Then
        ' If it is a weekday or holiday, push the days and due day up by a day and do nothing else
            StartDay = StartDay + 1
            EndDay = EndDay + 1
            EndTime = EndTime + 1
        Else
        ' If it is a workday, push the day up, but keep due the same
            EndDay = EndDay + 1
            If NumHours > 24 * (EndShift - StartShift) Then
            ' If you need more hours than is in a day, decrement the hours and push up the start date
                NumHours = NumHours - 24 * (EndShift - StartShift)
                StartDay = StartDay + 1
            End If
            If IsWorkday(StartDay, Holidays) = False And NumHours > (EndShift - StartShift) Then
               EndTime = EndTime + 1
            End If
        End If
    Wend
    
    WorkHours = StartDay + NumHours / 24
    
    End Function
    
    Function IsWorkday(MyDate As Date, Optional Holidays As Range) As Boolean
    Dim cl As Range
    Dim bRtn As Boolean
    Dim WkDay As Long
    
    bRtn = True
    
    If Not Holidays Is Nothing Then
        For Each cl In Holidays
            If Int(MyDate) = cl.Value Then
                bRtn = False
                Exit For
         End If
        Next
    End If
    
    WkDay = Weekday(MyDate, vbMonday)
    
    If WkDay > 5 Then
        bRtn = False
    End If
    
    IsWorkday = bRtn
           
    End Function

    Thank you for this answer, though I fully don't understand.
    Should I put all of this code as a formula in a cell?

    And how do I change the work hours?
    (Preferably getting this info from two seperate cells)


    /Jocce

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

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    First of all, the code should be in a macro-enabled workbook (XLSM or XLSB).

    Open the VB Editor (Alt-F11). Click on Insert -> Module and copy and paste the code in.

    Then you can use it as a regular formula in a cell like so: =WorkHours(A3,B3,7/24,16/24,Holidays)
    Cell A3 is the start time
    Cell B3 is the number of hours
    7/24 is the start time of the shift.
    16/24 is the end time of the shift.
    Holidays is a range containing the dates of holidays observed by your company. This argument is optional.

    The reason we divide the 7 and 16 by 24 is because Excel measures days as whole numbers since Jan 1, 1900 and the time part of date/time is a fraction so 12 hours is 0.5 days, etc. I'm not going to work out what 7/24 is, so I'll let excel do that. You could just as easily entered 7 AM into a cell, and then read the cell - in this case, you would not have to divide by 24 since Excel would have already done that.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    11-15-2016
    Location
    CENTRAL MARMOTTE
    MS-Off Ver
    2010
    Posts
    8

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    Quote Originally Posted by dflak View Post
    First of all, the code should be in a macro-enabled workbook (XLSM or XLSB).

    Open the VB Editor (Alt-F11). Click on Insert -> Module and copy and paste the code in.

    Then you can use it as a regular formula in a cell like so: =WorkHours(A3,B3,7/24,16/24,Holidays)
    Cell A3 is the start time
    Cell B3 is the number of hours
    7/24 is the start time of the shift.
    16/24 is the end time of the shift.
    Holidays is a range containing the dates of holidays observed by your company. This argument is optional.

    The reason we divide the 7 and 16 by 24 is because Excel measures days as whole numbers since Jan 1, 1900 and the time part of date/time is a fraction so 12 hours is 0.5 days, etc. I'm not going to work out what 7/24 is, so I'll let excel do that. You could just as easily entered 7 AM into a cell, and then read the cell - in this case, you would not have to divide by 24 since Excel would have already done that.

    Thank you. Though doesn't work. Gives correct date, but always 13:00 as time for Completion...


    /Jocce

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

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    Posting the attachment since the site crashed during the upload.
    Attached Files Attached Files

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

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    I apparently did not test this function that well. It should use 7 hours out of the first day (16 -9) leaving 8 hours into the second day 7 + 8 = 15:00, so it does have the wrong time. However, the time does change when you change the hours. I'll have to figure out where the two hour difference happens.

  8. #8
    Registered User
    Join Date
    11-15-2016
    Location
    CENTRAL MARMOTTE
    MS-Off Ver
    2010
    Posts
    8

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    Quote Originally Posted by dflak View Post
    I apparently did not test this function that well. It should use 7 hours out of the first day (16 -9) leaving 8 hours into the second day 7 + 8 = 15:00, so it does have the wrong time. However, the time does change when you change the hours. I'll have to figure out where the two hour difference happens.

    I also tried getting it to be using start / stop times from two different cells. This kind of works, but gives the wrong calculation sometimes. Havn't figured out at what times this happens. (Sometimes it also gives finishing time outside of work hours).


    It also would be great (but propably advanced) if it was possible to adjust times for different days in the week (in Mondays we have maintenance of the machine and on fridays we have different shift-times).
    But if this is to complicated we can work around it.
    (And perhaps calculate for brakes also?)


    /Jocce
    Last edited by Jocce; 11-17-2016 at 06:02 AM.

  9. #9
    Registered User
    Join Date
    11-15-2016
    Location
    CENTRAL MARMOTTE
    MS-Off Ver
    2010
    Posts
    8

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    Double post, sorry.
    Last edited by Jocce; 11-17-2016 at 06:01 AM. Reason: Double post

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

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    I'm working on a priority project. I'll get back to this early next week. I thought I had this worked out, but apparently not. I suspect it will be a minor change to the code.

  11. #11
    Registered User
    Join Date
    11-15-2016
    Location
    CENTRAL MARMOTTE
    MS-Off Ver
    2010
    Posts
    8

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    Quote Originally Posted by dflak View Post
    I'm working on a priority project. I'll get back to this early next week. I thought I had this worked out, but apparently not. I suspect it will be a minor change to the code.
    Thank you, that would be great.

    I though found a formula that works. Only problem is that I can't figure out how to get the holidays accounted for.

    This one I am using.
    In English:
    =WORKDAY(A1,CEILING((B1+MOD(A1,1)-D$2)/(E$2-D$2),1)-1)+MOD(A1,1)+B1-CEILING(MOD(A1,1)+B1-D$2,E$2-D$2)+E$2-D$2


    /Jocce

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

    Re: Calculating Due-date and time, Workdays, with specific work hours?

    Waiting for one of my programmers to come back from lunch.

    I think I got it working. Use the same syntax as you did before. Attached is my QA workbook. It demonstrates "hopping over" the Thanksgiving holiday (as defined in the holiday table) and also skipping the weekend.

    Option Explicit
    Function WorkHours(StartDate As Date, NumHours As Double, ShiftStart As Date, ShiftEnd As Date, _
        Optional Holidays As Range) As Date
    Application.Volatile
    Dim StartDay As Date            ' Start of the workday
    Dim EndDay As Date              ' End of the workday
    Dim EndHours As Date            ' Ending time of project
    Dim EndDate As Date             ' Ending date of project
    Dim DayHours As Double          ' Number of hours in the workday
    Dim BeginTime As Date           ' Beginning of task or shift
    
    ' Initialize variables
    EndDate = StartDate + NumHours / 24
    EndDay = Int(StartDate) + ShiftEnd
    BeginTime = StartDate - Int(StartDate)
    
    ' While the end date still exceeds the end of day for a workday
    While EndDate > EndDay
        ' increment EndDay
        EndDay = EndDay + 1
        ' Process only for workdays
        If IsWorkday(EndDay, Range("Holidays")) = True Then
            'decrement the hours
            NumHours = NumHours - 24 * (ShiftEnd - BeginTime)
            BeginTime = ShiftStart  ' Begin time becomes shift start after the first day
            ' Compute new enddate
            EndDate = Int(EndDay) + ShiftStart + NumHours / 24
        Else
            ' increment the end date for weekends and holidays without processing
            EndDate = EndDate + 1
        End If
    Wend
    
    WorkHours = EndDate
    End Function
    
    Function IsWorkday(MyDate As Date, Optional Holidays As Range) As Boolean
    Dim cl As Range             ' Pointer to holiday range
    Dim bRtn As Boolean         ' Return value (default = True unless proven otherwise)
    Dim WkDay As Long
    
    bRtn = True
    
    ' Check to see if the date is on the holiday list
    If Not Holidays Is Nothing Then
        For Each cl In Holidays
            If Int(MyDate) = cl.Value Then
                bRtn = False
                Exit For
         End If
        Next
    End If
    
    ' Check to see if the day is a weekend
    WkDay = Weekday(MyDate, vbMonday)
    
    If WkDay > 5 Then
        bRtn = False
    End If
    
    IsWorkday = bRtn
           
    End Function
    P.S. Columns A,B and D are the only ones worth looking at in the QA workbook. The other columns were intermediate steps in development. Ignore them.
    Attached Files Attached Files
    Last edited by dflak; 11-17-2016 at 04:31 PM. Reason: Add PS

+ 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. [SOLVED] Calculating Target Date/Time Based on Start date and hours - Excel 2007
    By chinraj in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 12-09-2019, 01:43 AM
  2. [SOLVED] Calculating Hours Between Two Date - Including Time
    By azieli02 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-07-2016, 10:40 PM
  3. Replies: 7
    Last Post: 03-08-2014, 05:31 PM
  4. Spell Out Workdays & Calculate Work Hours
    By mycon73 in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 02-08-2014, 01:43 AM
  5. Calculating workdays based on date/time received
    By Sharv103 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-18-2013, 08:46 PM
  6. Replies: 1
    Last Post: 06-18-2012, 06:54 AM
  7. Calculating time to track work hours
    By halvy52 in forum Excel General
    Replies: 3
    Last Post: 07-24-2009, 03:34 PM

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