On some days a worker multitasks. How do I account for overlapping tasks?

timmy2

New Member
Joined
Jul 17, 2017
Messages
15
Here's a worker's activity log. I am interested in adding up "minutes worked", but only minutes that do not overlap. In other words, multi-tasking is not rewarded, per se.

TaskWork startedWork endedMinutes workedDate
A5:00PM6:15PM751/13/2017
A2:00PM4:00PM1201/15/2017
B2:10PM2:20PM101/15/2017
C2:12PM2:17PM51/15/2017
D3:00PM5:00PM1201/15/2017
A1:00PM2:00PM601/17/2017

<tbody>
</tbody>

On some days he performs a single task so all the minutes count. (see 1/13 and 1/17)

But on some days he performs more than one task, like on 1/15. When he logs in he starts the first task of the day. Then he starts another task, which might be short (like task B) or it might extend beyond the end of the first task (like task D).

Given the rules I can't simply add up the minutes column.

In a new column I have tried numerous formulas using IF statements to either count the full minutes worked or reduce or eliminate them if they occur while other tasks are already being performed. If this were a program I could use a variable to compare to but doing this in Excel baffles me.

BTW, the worker is me, so don't feel sorry for him.
 
Timmy2,
Sorry for any confusion. Nothing you did wrong...
The code needed some 'housekeeping' with the 'n' value ... with those revisions it should work now.
Just replace the previous code with the following. Let me know how it goes.
Perpa

Code:
Sub AccumTime3()
Dim n, rw, rw1, LastRow As Long
LastRow = Cells(Rows.Count, "D").End(xlUp).Row
Cells(2, "E") = Cells(2, "C")
For rw = 3 To LastRow
    If Cells(rw - 1, "D") <> Cells(rw, "D") Then Cells(rw, "E") = Cells(rw, "C")
Next
n = 1
For rw1 = 3 To LastRow
    If Cells(rw1, "E") <> "" Then
        n = 1
        GoTo Nextrw
    End If
    
    If Cells(rw1, "E") = "" Then
        
        If Cells(rw1, "A") >= Cells(rw1 - n, "A") And Cells(rw1, "B") <= Cells(rw1 - n, "B") Then
            n = n + 1
            GoTo Nextrw     'No Additional time
        End If
        
        If Cells(rw1, "A") >= Cells(rw1 - n, "B") Then
        
            Cells(rw1 - n, "E") = Cells(rw1 - n, "E") + Cells(rw1, "C")     'Additional Time after Original Task Ends
            n = n + 1
            GoTo Nextrw
        End If
        
        If Cells(rw1, "A") >= Cells(rw1 - n, "A") And Cells(rw1, "B") > Cells(rw1 - n, "B") Then                  'Additional Time after Original TaskA Ends
            Cells(rw1 - n, "E") = Cells(rw1 - n, "E") + (Cells(rw1, "B") - Cells(rw1 - n, "B")) * 1440
            
        End If
        
    End If
    n = n + 1
Nextrw:
    
Next
End Sub
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Thank you, Perpa. There still seems to be a problem when sessions overlap. See screen capture below, where I've added a couple columns to account for overlapping minutes. NOTE that I changed the "work started" time in cell A11 to add another overlap occurrence for testing purposes.

On 1/15/2017, for example, the sessions in rows 10 and 11 effectively total 55 unique minutes (after removing overlap). Likewise, on 1/19/2017 the sessions in rows 13 and 14 effectively total 40 minutes. At least that's what it looks like to me.

0VltGgX.png
 
Upvote 0
Timmy2,
I see what you are doing now...I had not allowed for the overlap in subsequent times during the same day.
See if the code below does what you expected.
Perpa

Sheet2

A
B
C
D
E
1
Work started
Work ended
Minutes worked
Date
Total Minutes Worked
2
12:11 PM
2:58 PM
167
1/7/2017
319
3
3:33 PM
6:01 PM
148
1/7/2017
4
9:24PM
9:28 PM
4
1/7/2017
5
12:31 PM
2:41 PM
130
1/15/2017
198
6
12:35 PM
12:39 PM
4
1/15/2017
7
12:36 PM
12:38 PM
2
1/15/2017
8
7:03 PM
7:09 PM
6
1/15/2017
9
7:05 PM
7:58 PM
53
1/15/2017
10
9:04 PM
9:17 PM
13
1/15/2017
11
10:59 AM
11:20 AM
21
1/19/2017
40
12
11:09 AM
11:39 AM
30
1/19/2017
13
11:23 AM
11:30 AM
7
1/19/2017

<tbody>
</tbody>


Excel tables to the web >> http://www.excel-jeanie-html.de/index.php?f=1" target="_blank"> Excel Jeanie HTML 4


Code:
Sub AccumTime4()
Dim n, rw, rw1, LastRow As Long
LastRow = Cells(Rows.Count, "D").End(xlUp).Row
Cells(2, "E") = Cells(2, "C")
For rw = 3 To LastRow
    If Cells(rw - 1, "D") <> Cells(rw, "D") Then Cells(rw, "E") = Cells(rw, "C")
Next
n = 1
ET = Cells(2, "B")
For rw1 = 3 To LastRow
    If Cells(rw1, "E") <> "" Then
        n = 1
        ET = Cells(rw1, "B")    'Sets the End Time when column E <> ""
        GoTo Nextrw
    End If
    If Cells(rw1, "E") = "" Then
        'No Additional time
      If Cells(rw1, "A") >= Cells(rw1 - n, "A") And Cells(rw1, "B") <= ET Then
             If Cells(rw1 - n, "B") > ET Then ET = Cells(rw1 - n, "B")
             n = n + 1
            GoTo Nextrw
        End If
      
        'All Time is after Original Task Ends
        If Cells(rw1, "A") >= ET Then
            Cells(rw1 - n, "E") = Cells(rw1 - n, "E") + Cells(rw1, "C")
            ET = Cells(rw1, "B")
            n = n + 1
            GoTo Nextrw
        End If
        
        'Partial Additional Time after Original Task Ends
        If Cells(rw1, "A") >= Cells(rw1 - n, "A") And Cells(rw1, "B") > ET Then
            Cells(rw1 - n, "E") = Cells(rw1 - n, "E") + (Cells(rw1, "B") - ET) * 1440
            ET = Cells(rw1, "B")
            n = n + 1
        End If
        
    End If
Nextrw:
    
Next
End Sub
 
Upvote 0
Timmy2,
I see what you are doing now...I had not allowed for the overlap in subsequent times during the same day.
See if the code below does what you expected.
Perpa

Sheet2

ABCDE
1Work startedWork endedMinutes workedDateTotal Minutes Worked
212:11 PM2:58 PM1671/7/2017319
33:33 PM6:01 PM1481/7/2017
49:24PM9:28 PM41/7/2017
512:31 PM2:41 PM1301/15/2017198
612:35 PM12:39 PM41/15/2017
712:36 PM12:38 PM21/15/2017
87:03 PM7:09 PM61/15/2017
97:05 PM7:58 PM531/15/2017
109:04 PM9:17 PM131/15/2017
1110:59 AM11:20 AM211/19/201740
1211:09 AM11:39 AM301/19/2017
1311:23 AM11:30 AM71/19/2017

<tbody>
</tbody>

Perpa, I cannot thank you enough. You solved it! :biggrin:
 
Upvote 0
Perpa, I cannot thank you enough. You solved it! :biggrin:

Tim,
Very glad this works for you. You are quite welcome. Come back any time.
I will follow up with a brief explanation of the code logic tomorrow. Right now I am watching the NCAAF Bowl Championship game!
Perpa
 
Upvote 0
Tim,
Let's start with an annotated version of the code to indicate what it is doing at each step.
Have a look at it and then you can ask for clarification if you need to.
Perpa

Code:
Sub AccumTime4()
Dim n, rw, rw1, LastRow As Long
LastRow = Cells(Rows.Count, "D").End(xlUp).Row             [COLOR=#008000]'Finds the last row in the 'Date' column D[/COLOR]
Cells(2, "E") = Cells(2, "C")  [COLOR=#008000] 'Copies the first minutes for the first Date to column E
[/COLOR]
[COLOR=#008000][B]'This FOR/NEXT loop checks for a change in the Date[/B] and copies the first minutes for each new Date to to column E
'It compares in column D, the previous cell value in  row 'rw-1' value  to the current cell value in row 'rw'[/COLOR]
For rw = 3 To LastRow
    If Cells(rw - 1, "D") <> Cells(rw, "D") Then Cells(rw, "E") = Cells(rw, "C")
Next
'***********************

[COLOR=#008000]'This next part looks for blanks in column E left by the above FOR/NEXT loop
'n' keeps track of the number of rows since the last non-blank cell in column E, or the number of rows back to the first entry for that date
'ET' is the 'End Time' variable that changes value when the criteria is met in the 'IF' statements below for each Date
[/COLOR]
n = 1    [COLOR=#008000]'Sets the initial count to 1[/COLOR]
ET = Cells(2, "B")     [COLOR=#008000]'This sets ET to the value of the first End Time which is in row 2[/COLOR]
For rw1 = 3 To LastRow    [COLOR=#008000]'Looks at every row between row 3 and the last filled row[/COLOR]
    If Cells(rw1, "E") <> "" Then   [COLOR=#008000]'if the column E cell value is not blank[/COLOR]
        n = 1
        ET = Cells(rw1, "B")    [B][COLOR=#008000]'ReSets the End Time when column E value is not blank, new date[/COLOR][/B]
        GoTo Nextrw
    End If

[COLOR=#008000]'When column E values are blank then the IF statements check for 3 things: [B]NO Additional time, All Additional time, and Overlapping time[/B][/COLOR]
    If Cells(rw1, "E") = "" Then      [COLOR=#008000]' if the column E cell value is blank
[/COLOR]
       [COLOR=#008000][B]'This IF checks whether there was NO Additional time[/B], 'rw1-n' is the first row for any new date, as rw1 increases, the number of rows back 'n' to the first row for the day must increase as well
[/COLOR]
      If Cells(rw1, "A") >= Cells(rw1 - n, "A") And Cells(rw1, "B") <= ET Then    [COLOR=#008000]'If true then[/COLOR]
             If Cells(rw1 - n, "B") > ET Then ET = Cells(rw1 - n, "B")    [COLOR=#008000]'This line adjusts the value of ET if the previous cells value is greater than before[/COLOR]
             n = n + 1    [COLOR=#008000]'This line increments the number of rows since the last new Date[/COLOR]
            GoTo Nextrw    [COLOR=#008000]'Skips the next 2 IF statements since this IF was true[/COLOR]
        End If
      
        [B][COLOR=#008000]'This IF checks whether All the Time for a new task occurs after the Original Task Ends[/COLOR][/B]
        If Cells(rw1, "A") >= ET Then    [COLOR=#008000]'If true then[/COLOR]
            Cells(rw1 - n, "E") = Cells(rw1 - n, "E") + Cells(rw1, "C")   [COLOR=#008000]'Adds All the minutes in column C to the Date total minutes[/COLOR]
            ET = Cells(rw1, "B")    [COLOR=#008000]'Updates the End Time variable to the new End Time[/COLOR]
            n = n + 1      [COLOR=#008000]'This line increments the number of rows since the last new Date[/COLOR]
            GoTo Nextrw     [COLOR=#008000]'Skips the next IF statement since this IF was true[/COLOR]
        End If
        
        [B][COLOR=#008000]'This IF checks for Overlapping Durations[/COLOR][/B]
        If Cells(rw1, "A") >= Cells(rw1 - n, "A") And Cells(rw1, "B") > ET Then
            Cells(rw1 - n, "E") = Cells(rw1 - n, "E") + (Cells(rw1, "B") - ET) * 1440   [COLOR=#008000]'adds the extended minutes to the accumulated time[/COLOR]
            ET = Cells(rw1, "B")     [COLOR=#008000]'Updates the End Time variable to the new End Time[/COLOR]
            n = n + 1    [COLOR=#008000]'This line increments the number of rows since the last new Date[/COLOR]
        End If
        
    End If
Nextrw:    [COLOR=#008000]'This is where each of the above IF statements go to after they have run[/COLOR]
    
Next    [COLOR=#008000]'This takes you back up to the FOR which increments rw1 which takes you to the next row down[/COLOR]
End Sub   [COLOR=#008000]'This is where the macro terminates after all the rows have been checked according to the 3 criteria (IF statements).[/COLOR]
 
Upvote 0
timmy2,
Assuming '180' is the total you are trying to get on 1/15/2017, here is a macro solution.
The Times must be in 'Time format', meaning there needs to be is a space between the minutes and the 'PM',
and Excel recognizes them as 'Time', not as a 'General' number or 'Text'.
Perpa

Perpa, upon returning to my actual data (instead of test worksheets), I realize that the techniques I've been using to convert the source timesheet log into data usable in Excel are incompatible with what your macro needs (as you defined above).

GotomyPC Activity Logs provide Work Started times like the following:

1/15 12:36P
1/15 7:03P
1/19 11:09A
1/19 11:23A
2/9 9:52P

<tbody>
</tbody>

Before your macro came along I had been converting the above data into Excel Date/Time values. (And since GotomyPC provides the session duration (albeit in a cryptic "2H 15M" form) I am able to calculate a Work Ended time and Duration in minutes.)

Now, given the existence of your wonderful macro I need only convert the GotomyPC data into what your macro needs.

Here's where I get confused:

I had been converting the GotomyPC data into Excel time and date values that are serial numbers. Displaying these serial numbers as hours, minutes, with AM or PM, space or not -- that's all just formatting for display purposes, but your macro apparently expects to see time values actually consisting of hours:minutes followed by a space followed by AM or PM. I initially tried using TEXT(A2,"H:MM AM/PM") to convert my data into what your Macro needs, but your macro choked on it because it's text value instead of a time value.

Would you mind giving me a hint as to how to most efficiently get from my original data to what your macro needs?
 
Upvote 0
Tim,
Let's start with an annotated version of the code to indicate what it is doing at each step.
Have a look at it and then you can ask for clarification if you need to.
Perpa

Awesome, Perpa! Perfectly clear. Stepping through your macro using the Debug tools and assigning watches helped me see your machine munch through it. (Is there a way to have values displayed according to format settings in the Locals or Watches windows? At times I typed the values for ET or Cells(rw1,"A") into an empty cell and set its format to Custom H:MM AM/PM so I could see what a value like .87777 equals in "AM/PM time".)

I'm embarrassed to admit that I did not realize how using VBA can overcome the limitations of single-cell formulas. Am I correct in believing that this problem could not have been solved with a formula? Opens up a world of possibilities for me. Thank you!

Your contribution here is testament to the good that the Internet offers.
 
Upvote 0
Tim,
Very glad I was able to show you some functionality with VBA with a practical application. There are probably formulae that others could help you with to accomplish a similar result. I prefer the macro approach to solving these problems because I can follow the logic more readily.

Regarding your question:
"Is there a way to have values displayed according to format settings in the Locals or Watches windows? At times I typed the
values for ET or Cells(rw1,"A") into an empty cell and set its format to Custom H:MM AM/PM so I could see what a value like
.87777 equals in "AM/PM time".

I would point you to an earlier discussion on this Forum at the following link, Post#3 thereof:

https://www.mrexcel.com/forum/excel...ml?highlight='fractional+time+to+hh:mm+AM/PM'

I hope you find it helpful.
Regards and enjoy coding!
Perpa
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top