Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Help with Excel formula 
Author Message
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 8:57 pm
Posts: 2220
Location: Here for now...
Reply with quote
Hi all,

Right. Excel problem....I've been playing with it for a while but can't quite figure it out...

I need to embed calculations to automatically give me the duration for a Priority 1 incident.

What we have to work with: create date/time and close date/time

The duration time works on a
· 9 hour day
· 8:00 to 17:00
· Weekends are excluded
Hours outside these times are excluded from the calculation.

I can't work out how to attached a file, so basically what I have is...1/1/1900 4.30 AM and 3/1/1900 7.30PM in all cells. A1 and B1 are formatted for date and C1 and D1 for time. So you get: 1/1/1900 - 3/1/1900 - 4.30AM - 7.30PM

If this doesn't make sense, or anyone needs me to PM them the file let me know.

Thanks for any help in advance!
PS Something special to whoever solves it (I'll write a new Forum Story with them as the main character or something)


Edit: Also, if possible, I'd like the outcome to be something like 2 days 13 hours. (Closest to 15 minutes, sorry I'm being picky but it's important)

_________________
ProfessorF wrote:
(The author of this post assumes no responsibility for any jail time served by anyone making use of this suggestion.)


Mon Jun 07, 2010 11:24 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
You should use the actual year as well, otherwise it won't calculate properly over year-end...

I'll see if I can have a look at it later.

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Tue Jun 08, 2010 5:46 am
Profile ICQ
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 8:57 pm
Posts: 2220
Location: Here for now...
Reply with quote
Thanks Dave, the actual year is in there too.

_________________
ProfessorF wrote:
(The author of this post assumes no responsibility for any jail time served by anyone making use of this suggestion.)


Tue Jun 08, 2010 6:49 am
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
Can you not simply use a date/time field for logged and a date/time field for resolved? As opposed to having 2 separate cells for date and time? That could make the calculation a bit easier to work with (I think, just starting to look at it now).

Edit: Closer reading, you have the complete date and time in all cells, just showing different aspects in A/B and C/D?

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Tue Jun 08, 2010 6:59 am
Profile ICQ
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
Okay, harder than I thought... I think the easiest way is with a custom function (unless anyone else has any ideas).

I've got the function working correctly for jobs that start and end on the same day. Now need to calculate in days and weekends...

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Tue Jun 08, 2010 8:14 am
Profile ICQ
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Which version of Excel?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Jun 08, 2010 8:19 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
OK, done.

It's a lot easier with Excel 2007 though as you can use the NETWORKDAYS() function to exclude weekends I don't think there's an easy way to do that in 2003.

I haven't compressed it down so you can see all the working. You should be able to get it straight out into Net Days and Net Hours though.

Using this format...

Code:
Date From, Date To, Time From, Time To, Gross Days, Gross Hours, Total Time, Net Days, Net Hours

Gross Days = NETWORKDAYS(Date From, Date To)
Gross Hours = Time To - Time From (format as number with 2 dec places)
Total Time = Gross Days + Gross Hours
Net Days = TRUNC(Total Time)
Net Hours = 24 * (Total Time - Net Days)


Net Days in your example shows 2 and net hours shows 15.

Also works when time from is after time to (i.e. removes a day and adds time, etc...)

Hope this helps.
:)

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Jun 08, 2010 8:40 am
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
It doesn't work, if you are on the same day! (E.g. a call is openened on 01.06.2010 and is closed on 01.06.2010, the NETWORKDAYS function still returns 1!)

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Tue Jun 08, 2010 9:27 am
Profile ICQ
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
big_D wrote:
It doesn't work, if you are on the same day! (E.g. a call is openened on 01.06.2010 and is closed on 01.06.2010, the NETWORKDAYS function still returns 1!)

LOL :D

OK, I'll work on it :D

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Jun 08, 2010 9:28 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
OK,

Code:
Gross Days = IF(Date From = Date To, 0, NETWORKDAYS(Date From, Date To))


That should do it :D

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Jun 08, 2010 9:38 am
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
So, pretty much have a programmed solution... Post it in a bit.

One quick question, do you count 9 hours as 1 day or 24 hours as one day?

I am doing whole days as simply + 1, but when the start time on the first day and the end time on the last day would make 9 hours (or more) in total, do you want that to be +1 day or +9 hours?

E.g. 01/06/2010 04:30 to 04/06/2010 is giving, currently, 4 days and 9 hours, should that be so, or 5 days?

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Tue Jun 08, 2010 11:50 am
Profile ICQ
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
So, it is still a bit quick and dirty, and you'll need to do some proper testing, I've used a couple of simple dates and it seems to work. It returns a string, currently, of days:hours:minutes. We can change this as needed to, to be a date for example.

It excludes weekends. It works works correctly with the working day of 9 hours (truncates times outside of 08:00 to 17:00), these are the constants at the top of the function. You can change these as needed.

It works on 24 hour days! If you want 9 hour days, pay attention to the comment at the end to recalculate the number of days and hours in 9s (one formula needs changing). E.g. currently 01/06/2010 07:30 until 08/06/2010 comes out at 6 days, 9 hours, changing the 24 for 9 in the calculation would give 7 days!

To set this up, open the VBA editor in Excel (Alt+F11) and insert a new module. Then copy and paste the code below into the module and save it.

Then, on the worksheet, simply enter the function name into the required cell: E.g. A1 = start date/time, B1 = end date/time, C1 would then be =elapsed(A1,B1)

If you really need to split out the date and time into separate cells, you will need a combo cell with it put back together or you will need to re-write the function to accept the date and time broken up.

Code:
Option Explicit

'
' Takes the start date of a call and the end date of the call and calculates the number
' of hours duration of the incident.
'
' Weekends are excluded.
' Hours outside of the defined working day are ignored
'
Function elapsed(startDate As Date, endDate As Date) As String

    Const cnWorkingHours As Integer = 9
    Const cnStartOfDay As Integer = 8
    Const cnEndOfDay As Integer = 17
   
    Dim nStartDay       As Integer
    Dim nClearedDay     As Integer
    Dim nStartMonth     As Integer
    Dim nClearedMonth   As Integer
    Dim nEndHour        As Integer
    Dim nEndMinute      As Integer
    Dim nStartHour      As Integer
    Dim nStartMinute    As Integer
    Dim nElapsedDays    As Integer
    Dim nElapsedHours   As Integer
    Dim nElapsedMinutes As Integer
    Dim nCount          As Integer
    Dim dWorkingDate    As Date
   
    '
    ' Used to work out if everything took place on one day. Month only needed, in case
    ' the problem took exactly 1 month to fix!!
    '
    nStartDay = Day(startDate)
    nClearedDay = Day(endDate)
    nStartMonth = Month(startDate)
    nClearedMonth = Month(endDate)
    '
    ' Set up the start and end times for the calculations. If the start time is before
    ' the start of the day, set it to the start of day (E.g. 04:30 become 08:00). Do
    ' the same for the end time, compared to end of working day.
    '
    If Hour(startDate) > cnStartOfDay Then
        nStartHour = Hour(startDate)
        nStartMinute = Minute(startDate)
    Else
        nStartHour = cnStartOfDay
        nStartMinute = 0
    End If
    If Hour(endDate) < cnEndOfDay Then
        nEndHour = Hour(endDate)
        nEndMinute = Minute(endDate)
    Else
        nEndHour = cnEndOfDay
        nEndMinute = 0
    End If

    '
    ' Check to see if everything occurs on 1 days:
    '
    If nStartDay = nClearedDay And nStartMonth = nClearedMonth Then
        '
        ' Simple calculation, it took less than a day!
        '
        nElapsedDays = 0
        nElapsedHours = nEndHour - nStartHour
        '
        ' We need to adjust the minutes and hours, if the call wasn't logged on
        ' the hour.
        '
        If nStartMinute <= nEndMinute Then
            '
            ' We only need to adjust the minutes
            '
            nElapsedMinutes = nEndMinute - nStartMinute
        Else
            nElapsedHours = nElapsedHours - 1
            nElapsedMinutes = (60 + nEndMinute) - nStartMinute
        End If
    Else
        '
        ' We need to go through each day and add in the hours, excluding
        ' weekends...
        '
        nElapsedDays = 0
        nElapsedHours = 0
        nElapsedMinutes = 0
        dWorkingDate = startDate
        '
        ' Loop through each day, adding 1 to the day count (with the exception of the
        ' first day, there we need to calculate whether it was a full day or whether
        ' we need to add the correct number of hours and minutes.
        '
        Do
            '
            ' If it is the first day, do the calculations...
            '
            If dWorkingDate = startDate Then
                If nStartHour = cnStartOfDay And nStartMinute = 0 Then
                    nElapsedDays = nElapsedDays + 1
                Else
                    nElapsedHours = cnEndOfDay - nStartHour
                    If nStartMinute <> 0 Then
                        nElapsedHours = nElapsedHours - 1
                        nElapsedMinutes = 60 - nStartMinute
                    End If
                End If
            Else
                '
                ' Otherwise, simply increment the day counter, if it isn't a weekend.
                '
                If Weekday(dWorkingDate) <> 1 And Weekday(dWorkingDate) <> 7 Then
                    nElapsedDays = nElapsedDays + 1
                End If
            End If
            '
            ' Add 1 day to the working date
            '
            dWorkingDate = DateSerial(Year(dWorkingDate), Month(dWorkingDate), Day(dWorkingDate) + 1)
        Loop While dWorkingDate <= endDate
       
        '
        ' Now add in the hours and minutes for the last day - the loop above loops
        ' through all days, until the last day.
        '
        nElapsedHours = nElapsedHours + (nEndHour - cnStartOfDay)
        nElapsedMinutes = nElapsedMinutes + nEndMinute
       
        If nElapsedMinutes > 60 Then
            nElapsedMinutes = nElapsedMinutes - 60
            nElapsedHours = nElapsedHours + 1
        End If
        '
        ' NOTE : For working days, replace 24 with the cnWorkingHours!
        '
        If nElapsedHours > 24 Then
            nElapsedHours = nElapsedHours - 24
            nElapsedDays = nElapsedDays + 1
        End If
    End If
   
    elapsed = CStr(nElapsedDays) & ":" & CStr(nElapsedHours) & ":" & CStr(nElapsedMinutes)

End Function

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Tue Jun 08, 2010 12:16 pm
Profile ICQ
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
Erm, Blue? Did that help?

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Wed Jun 09, 2010 7:25 am
Profile ICQ
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 8:57 pm
Posts: 2220
Location: Here for now...
Reply with quote
Sorry for the delay guys - been busy moving into a new apartment and have been in and out of the office. I had it to the point where I could get it working if it closed and opened on the same day but no further.

Will give the below a go shortly and see how it goes.

Will update soon - thanks for the help guys, appreciated!

_________________
ProfessorF wrote:
(The author of this post assumes no responsibility for any jail time served by anyone making use of this suggestion.)


Wed Jun 09, 2010 10:43 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 8:57 pm
Posts: 2220
Location: Here for now...
Reply with quote
Ok Dave - I've created a module- pasted your code in...no problems so far.

However I can't quiet work out how to 'call' the functions into the worksheet.

I've tried just entering the function name, or putting an = before. I think I might be being a simpleton and therefor might need some more detailed step by step instructions. :oops:

_________________
ProfessorF wrote:
(The author of this post assumes no responsibility for any jail time served by anyone making use of this suggestion.)


Wed Jun 09, 2010 11:23 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 19 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 41 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.