How to calculate difference between 2 dates/time excluding weekends ? (1 Viewer)

sanzoo

Registered User.
Local time
Today, 20:53
Joined
Mar 24, 2012
Messages
18
Hello guys,
I desperately need your help with my query which supposed to provide me with an information about hours between 2 dates/time, the only hook is that I would need to somehow get rid of all the weekends between the dates because the idea of this query is to track SLA and weekends can not be calculated within the final number as our regular working days are from mon-fri. So basically I have 2 dates/time stamps one next to other(see attached example) and I have calculated the difference between these two using Datediff function but dont know how to exclude weekends from it. Any idea how this could be possible?

Many thanks.
 

Attachments

  • sample.xls
    28.5 KB · Views: 356
Last edited:

sanzoo

Registered User.
Local time
Today, 20:53
Joined
Mar 24, 2012
Messages
18
Thank John indeed for your code. Unfortunatelly Im not very familiar with VBA however I think it shoudnt be a problem to copy and paste this code inside but my question is do I need to rename any names inside the code like variables etc. accorind to my naming and vice versa and if which ones ?
thanks
 

John Big Booty

AWF VIP
Local time
Tomorrow, 04:53
Joined
Aug 29, 2005
Messages
8,263
You'll need to copy and paste the code into a Module and then call that module whenever you need to calculate the number of days.
 

sanzoo

Registered User.
Local time
Today, 20:53
Joined
Mar 24, 2012
Messages
18
Well I did exactly what you told me I pasted the code into the new module and saved it but dont know how to call it specificaly with my query which result I have attached in my thread "sample.xls". I guess I need to somehow specify which fileds are needed to be calculated inside the code otherwise it wont work ? Maybe I need to rename the filed names inside the code according to my query or vice versa in order to let VBA know what fields have to be calculated. Sorry maybe I sound silly but I really need this to be solved.

thank you for your support
 

John Big Booty

AWF VIP
Local time
Tomorrow, 04:53
Joined
Aug 29, 2005
Messages
8,263
If you want to show the day difference on a form use the following in the Control Source of an unbound text box;
Code:
=dhCountWorkdaysA([YourStartDate],[YourEndDate])
In a query put the following in the top row of a blank column in your query design grid;
Code:
dhCountWorkdaysA([Text2],[Text4])

This refers to the following portion of the code from the link initially provided;
Code:
Public Function dhCountWorkdaysA(ByVal dtmStart As Date, ByVal dtmEnd As Date, _
 Optional adtmDates As Variant = Empty) _
 As Integer

   [COLOR="DarkGreen"] ' Count the business days (not counting weekends/holidays) in
    ' a given date range.
    
    ' Modified from code in
    ' "Visual Basic Language Developer's Handbook"
    ' by Ken Getz and Mike Gilbert
    ' Copyright 2000; Sybex, Inc. All rights reserved.
    
    ' Requires:
    '   SkipHolidays
    '   CountHolidays
    '   IsWeekend
    
    ' In:
    '   dtmStart:
    '       Date specifying the start of the range (inclusive)
    '   dtmEnd:
    '       Date specifying the end of the range (inclusive)
    '       (dates will be swapped if out of order)
    '   adtmDates (Optional):
    '       Array containing holiday dates. Can also be a single
    '       date value.
    ' Out:
    '   Return Value:
    '       Number of working days (not counting weekends and optionally, holidays)
    '       in the specified range.
    ' Example:
    '   Debug.Print dhCountWorkdaysA(#7/2/2000#, #7/5/2000#, _
    '    Array(#1/1/2000#, #7/4/2000#))
    '
    '   returns 2, because 7/2/2000 is Sunday, 7/4/2000 is a holiday,
    '   leaving 7/3 and 7/5 as workdays.[/COLOR]
    
    Dim intDays As Integer
    Dim dtmTemp As Date
    Dim intSubtract As Integer
    
    [COLOR="DarkGreen"]' Swap the dates if necessary.>[/COLOR]
    If dtmEnd < dtmStart Then
        dtmTemp = dtmStart
        dtmStart = dtmEnd
        dtmEnd = dtmTemp
    End If
    
    [COLOR="DarkGreen"]' Get the start and end dates to be weekdays.[/COLOR]
    dtmStart = SkipHolidaysA(adtmDates, dtmStart, 1)
    dtmEnd = SkipHolidaysA(adtmDates, dtmEnd, -1)
    If dtmStart > dtmEnd Then
       [COLOR="DarkGreen"] ' Sorry, no Workdays to be had. Just return 0.[/COLOR]
        dhCountWorkdaysA = 0
    Else
        intDays = dtmEnd - dtmStart + 1
        
       [COLOR="DarkGreen"] ' Subtract off weekend days.  Do this by figuring out how
        ' many calendar weeks there are between the dates, and
        ' multiplying the difference by two (because there are two
        ' weekend days for each week). That is, if the difference
        ' is 0, the two days are in the same week. If the
        ' difference is 1, then we have two weekend days.[/COLOR]
        intSubtract = (DateDiff("ww", dtmStart, dtmEnd) * 2)
        
       [COLOR="DarkGreen"] ' The answer to our quest is all the weekdays, minus any
        ' holidays found in the table.[/COLOR]
        intSubtract = intSubtract + _
         CountHolidaysA(adtmDates, dtmStart, dtmEnd)
        
        dhCountWorkdaysA = intDays - intSubtract
    End If
End Function
 

sanzoo

Registered User.
Local time
Today, 20:53
Joined
Mar 24, 2012
Messages
18
Hi John, thanks for the detailed guide however when I ran the function from the query I ended up with following error message 'Compile error: Sub or Function not defined.'. VBA debuger highlighted the first lines where is the function name and 'SkipHolidaysA' in the middle of the script(below). I m wondering if its not caused by the fact that I have Date\Time field which Id like to analyze and not only Date. I would need hours instead of days so question is if there is any other code similar to NETWORKDAYS in excel which can provide me with an information about business hours not only days. I searched on the internet for another codes but no one really worked for me. Anyways thanks indeed for your help at least I learned something new.
Public Function dhCountWorkdaysA(ByVal dtmStart As Date, ByVal dtmEnd As Date, _ Optional adtmDates As Variant = Empty) _ As Integer
.
.
.
dtmEnd = SkipHolidaysA(adtmDates, dtmEnd, -1)
 

John Big Booty

AWF VIP
Local time
Tomorrow, 04:53
Joined
Aug 29, 2005
Messages
8,263
Have a look at the attached.
 

Attachments

  • db3.zip
    41.7 KB · Views: 674

sanzoo

Registered User.
Local time
Today, 20:53
Joined
Mar 24, 2012
Messages
18
Awesome.Thanks John.It works perfectly but one think is unclear to me for instance I have one start date 4.12.2012 and end date 27.12.2012 so it counts 18 days in between but it suppossed to be only 15 as we had 3 day holidays but when I put holiday days into the table and use it in the formula its always giving me 17 insted of 15 and dont know why. Do you have any idea ? Also Im wondering if the code could be somehow adjusted in order to get working hours insted of days.
Thank you very much.
 

John Big Booty

AWF VIP
Local time
Tomorrow, 04:53
Joined
Aug 29, 2005
Messages
8,263
I'm not a hundred percent sure but I'd guess you need to enumerate each day of the holiday not just the first.
 

mrsanzoo

New member
Local time
Today, 11:53
Joined
May 19, 2012
Messages
8
Ok that sounds logical I wanted to add all the dates into query formula just behind the end date but it only worked with one date for example "Work Days: dhCountWorkdaysA([strdate],[EndDate],#12/24/2012#), this was fine it counted 17 days ...but once I wanted to do following :dhCountWorkdaysA([strdate],[EndDate],(#12/24/2012#,#12/25/2012#)) it showed the error message even when I added Array name before the parentheses for holiday days.
 

sandupas

New member
Local time
Today, 11:53
Joined
Feb 20, 2014
Messages
1
Hi all,

Just want to give a lot of thanks to John, and add a little formula in addition here.
Once you have in place and working his function
mine:
WorkDays: dhCountWorkdaysA([Log Date],[Close Date])

you can calculate working hours between two intervals with this formula

WorkHours: FormatNumber(Switch([WorkDays]=1,([Close Date]-[Log Date])*24,[WorkDays]=2,((DateValue([Log Date])+TimeValue("05:30PM")-[Log Date])*24)+(8.5-((DateValue([Close Date])+TimeValue("05:30PM")-[Close Date])*24)),[WorkDays]>2,(([WorkDays]-1)*8)+((DateValue([Log Date])+TimeValue("05:30PM")-[Log Date])*24)+(8.5-((DateValue([Close Date])+TimeValue("05:30PM")-[Close Date])*24))))
 

Users who are viewing this thread

Top Bottom