Calculate time in excel

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
134
I get a text report from our time keeping system that i dump into excel through a data connection and I need to calculate the total hours for a day, the problem is, some employees clock in and out for lunch, so there could be up to 4 punches per day per employee. I am trying to automate it so that it will subtract the clockin time from the lunch clock out time, and the lunch clock in from the end of the day clockout to give me total hours worked for the day. Example, if I have these 4 times:
7.82
12.60
13.02
16.66
I need to subtract 7.82 from 12.60 and 13.02 from 16.62, and then add the two numbers together for the total for the day. I have no problem doing this if everyone clocked 4 punches a day, but some clock 2 times, and others 4, just depends on whether they leave for lunch. I get the data with a date and time stamp, is there a way to do this either with vba or a formula? I need excel to recognize whether there are 2 or 4 punches so it will know what to subtract from what? Any help is greatly appreciated! Here is an example of what the data looks like:


199992021-04-05 07:49:19.000CLARKLEAH07:49:19.000
199992021-04-05 12:36:11.000CLARKLEAH12:36:11.000
199992021-04-05 13:01:25.000CLARKLEAH13:01:25.000
199992021-04-05 16:39:43.000CLARKLEAH16:39:43.000
199992021-04-06 07:46:02.000CLARKLEAH07:46:02.000
199992021-04-06 12:30:33.000CLARKLEAH12:30:33.000
199992021-04-06 13:06:53.000CLARKLEAH13:06:53.000
199992021-04-06 16:46:06.000CLARKLEAH16:46:06.000
199992021-04-07 07:55:45.000CLARKLEAH07:55:45.000
199992021-04-07 11:38:46.000CLARKLEAH11:38:46.000
199992021-04-07 12:10:14.000CLARKLEAH12:10:14.000
199992021-04-07 16:30:03.000CLARKLEAH16:30:03.000
199992021-04-08 07:50:41.000CLARKLEAH07:50:41.000
199992021-04-08 16:34:03.000CLARKLEAH16:34:03.000
199992021-04-09 07:18:18.000CLARKLEAH07:18:18.000

I convert it 100 minute time also.
 

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
134
what happened to the time in Column "E" you had it there in your first example?

if its not there, than add the second line below.

Cells(r, "F") = Split(Cells(r, "B"), " ")(0) 'this will write the date into column F
Cells(r, "E") = Split(Cells(r, "B"), " ")(1) 'this will write the Time to column E
I removed the time from Column E, and added the second line of vba, it is placing the time in column E, but still getting 0's in Column G
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,170
add this line in anyway, it will add the time to column E and everything should work fine

Cells(r, "E") = Split(Cells(r, "B"), " ")(1) 'this will write the Time to column E
 

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
134
add this line in anyway, it will add the time to column E and everything should work fine

Cells(r, "E") = Split(Cells(r, "B"), " ")(1) 'this will write the Time to column E
I removed the time from Column E, and added the second line of vba, it is placing the time in column E, but still getting 0's in Column G:
1618253816463.png
 

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,170
this line should be

Cells(r, "E") = "." & Split(Cells(r, "B"), " ")(1) 'this will write the Time to column E
 

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,170

ADVERTISEMENT

actually you can just use the 3 line below

VBA Code:
Sub do_it()

For r = 1 To Cells(Rows.Count, "A").End(xlUp).Row Step 2
Cells(r, "H") = (Cells(r + 1, "E") - Cells(r, "E")) * 24 'this will write the hours into column H
Next r

End Sub
 

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
134
actually you can just use the 3 line below

VBA Code:
Sub do_it()

For r = 1 To Cells(Rows.Count, "A").End(xlUp).Row Step 2
Cells(r, "H") = (Cells(r + 1, "E") - Cells(r, "E")) * 24 'this will write the hours into column H
Next r

End Sub
ITs still giving zero's for the totals
 

Toadstool

Well-known Member
Joined
Mar 5, 2018
Messages
1,464
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

Hi JoeyGaspard,

Here's my solution using Functions. Data is your exprted time sheets with headings in row 1:
JoeyGaspard.xlsx
BCDE
1Clock Date/TimeSurnameFirst NameRepeated Time
22021-04-05 07:49:19.000CLARKLEAH07:49:19.000
32021-04-05 12:36:11.000CLARKLEAH12:36:11.000
42021-04-05 13:01:25.000CLARKLEAH13:01:25.000
52021-04-05 16:39:43.000CLARKLEAH16:39:43.000
62021-04-06 07:46:02.000CLARKLEAH07:46:02.000
72021-04-06 12:30:33.000CLARKLEAH12:30:33.000
82021-04-06 13:06:53.000CLARKLEAH13:06:53.000
92021-04-06 16:46:06.000CLARKLEAH16:46:06.000
102021-04-07 07:55:45.000CLARKLEAH07:55:45.000
112021-04-07 11:38:46.000CLARKLEAH11:38:46.000
122021-04-07 12:10:14.000CLARKLEAH12:10:14.000
132021-04-07 16:30:03.000CLARKLEAH16:30:03.000
142021-04-08 07:50:41.000CLARKLEAH07:50:41.000
152021-04-08 16:34:03.000CLARKLEAH16:34:03.000
Data


Here's the Results tab with the totals per day
Cell Formulas
RangeFormula
A2:A15A2=Data!A2
B2:B15B2=DATEVALUE(Data!B2)
C2:C15C2=TIMEVALUE(Data!B2)
D2:D15D2=CHOOSE(COUNTIFS($A$1:$A2,A2,$B$1:$B2,B2),"",24*(C2-INDEX($C$2:$C$16,AGGREGATE(15,6,ROW($C$2:$C$16)-ROW($C$1)/(($A$2:$A$16=A2)*($B$2:$B$16=B2)),1))),"",24*(C2-INDEX($C$2:$C$16,AGGREGATE(15,6,ROW($C$2:$C$16)-ROW($C$1)/(($A$2:$A$16=A2)*($B$2:$B$16=B2)),3))))
E2:E15E2=IF(COUNTIFS($A$1:$A2,A2,$B$1:$B2,B2)=COUNTIFS($A$1:$A$16,A2,$B$1:$B$16,B2),SUMIFS($D$2:$D$16,$A$2:$A$16,A2,$B$2:$B$16,B2),"")
 
Solution

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
134
Hi JoeyGaspard,

Here's my solution using Functions. Data is your exprted time sheets with headings in row 1:
JoeyGaspard.xlsx
BCDE
1Clock Date/TimeSurnameFirst NameRepeated Time
22021-04-05 07:49:19.000CLARKLEAH07:49:19.000
32021-04-05 12:36:11.000CLARKLEAH12:36:11.000
42021-04-05 13:01:25.000CLARKLEAH13:01:25.000
52021-04-05 16:39:43.000CLARKLEAH16:39:43.000
62021-04-06 07:46:02.000CLARKLEAH07:46:02.000
72021-04-06 12:30:33.000CLARKLEAH12:30:33.000
82021-04-06 13:06:53.000CLARKLEAH13:06:53.000
92021-04-06 16:46:06.000CLARKLEAH16:46:06.000
102021-04-07 07:55:45.000CLARKLEAH07:55:45.000
112021-04-07 11:38:46.000CLARKLEAH11:38:46.000
122021-04-07 12:10:14.000CLARKLEAH12:10:14.000
132021-04-07 16:30:03.000CLARKLEAH16:30:03.000
142021-04-08 07:50:41.000CLARKLEAH07:50:41.000
152021-04-08 16:34:03.000CLARKLEAH16:34:03.000
Data


Here's the Results tab with the totals per day
Cell Formulas
RangeFormula
A2:A15A2=Data!A2
B2:B15B2=DATEVALUE(Data!B2)
C2:C15C2=TIMEVALUE(Data!B2)
D2:D15D2=CHOOSE(COUNTIFS($A$1:$A2,A2,$B$1:$B2,B2),"",24*(C2-INDEX($C$2:$C$16,AGGREGATE(15,6,ROW($C$2:$C$16)-ROW($C$1)/(($A$2:$A$16=A2)*($B$2:$B$16=B2)),1))),"",24*(C2-INDEX($C$2:$C$16,AGGREGATE(15,6,ROW($C$2:$C$16)-ROW($C$1)/(($A$2:$A$16=A2)*($B$2:$B$16=B2)),3))))
E2:E15E2=IF(COUNTIFS($A$1:$A2,A2,$B$1:$B2,B2)=COUNTIFS($A$1:$A$16,A2,$B$1:$B$16,B2),SUMIFS($D$2:$D$16,$A$2:$A$16,A2,$B$2:$B$16,B2),"")
Thank you so much! THIS WORKS GREAT!!!
 

Toadstool

Well-known Member
Joined
Mar 5, 2018
Messages
1,464
Office Version
  1. 2016
Platform
  1. Windows
You're welcome!
Thanks for the feedback.
 

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
134
Hi JoeyGaspard,

Here's my solution using Functions. Data is your exprted time sheets with headings in row 1:
JoeyGaspard.xlsx
BCDE
1Clock Date/TimeSurnameFirst NameRepeated Time
22021-04-05 07:49:19.000CLARKLEAH07:49:19.000
32021-04-05 12:36:11.000CLARKLEAH12:36:11.000
42021-04-05 13:01:25.000CLARKLEAH13:01:25.000
52021-04-05 16:39:43.000CLARKLEAH16:39:43.000
62021-04-06 07:46:02.000CLARKLEAH07:46:02.000
72021-04-06 12:30:33.000CLARKLEAH12:30:33.000
82021-04-06 13:06:53.000CLARKLEAH13:06:53.000
92021-04-06 16:46:06.000CLARKLEAH16:46:06.000
102021-04-07 07:55:45.000CLARKLEAH07:55:45.000
112021-04-07 11:38:46.000CLARKLEAH11:38:46.000
122021-04-07 12:10:14.000CLARKLEAH12:10:14.000
132021-04-07 16:30:03.000CLARKLEAH16:30:03.000
142021-04-08 07:50:41.000CLARKLEAH07:50:41.000
152021-04-08 16:34:03.000CLARKLEAH16:34:03.000
Data


Here's the Results tab with the totals per day
Cell Formulas
RangeFormula
A2:A15A2=Data!A2
B2:B15B2=DATEVALUE(Data!B2)
C2:C15C2=TIMEVALUE(Data!B2)
D2:D15D2=CHOOSE(COUNTIFS($A$1:$A2,A2,$B$1:$B2,B2),"",24*(C2-INDEX($C$2:$C$16,AGGREGATE(15,6,ROW($C$2:$C$16)-ROW($C$1)/(($A$2:$A$16=A2)*($B$2:$B$16=B2)),1))),"",24*(C2-INDEX($C$2:$C$16,AGGREGATE(15,6,ROW($C$2:$C$16)-ROW($C$1)/(($A$2:$A$16=A2)*($B$2:$B$16=B2)),3))))
E2:E15E2=IF(COUNTIFS($A$1:$A2,A2,$B$1:$B2,B2)=COUNTIFS($A$1:$A$16,A2,$B$1:$B$16,B2),SUMIFS($D$2:$D$16,$A$2:$A$16,A2,$B$2:$B$16,B2),"")
Sorry to bother you, but could you help me out a little with this? I am having some trouble with it?
 

Watch MrExcel Video

Forum statistics

Threads
1,130,119
Messages
5,640,218
Members
417,131
Latest member
Seanr19871

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
Top