# Calculate time in excel

#### JoeyGaspard

##### Board Regular
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:

 19999 2021-04-05 07:49:19.000 CLARK LEAH 07:49:19.000 19999 2021-04-05 12:36:11.000 CLARK LEAH 12:36:11.000 19999 2021-04-05 13:01:25.000 CLARK LEAH 13:01:25.000 19999 2021-04-05 16:39:43.000 CLARK LEAH 16:39:43.000 19999 2021-04-06 07:46:02.000 CLARK LEAH 07:46:02.000 19999 2021-04-06 12:30:33.000 CLARK LEAH 12:30:33.000 19999 2021-04-06 13:06:53.000 CLARK LEAH 13:06:53.000 19999 2021-04-06 16:46:06.000 CLARK LEAH 16:46:06.000 19999 2021-04-07 07:55:45.000 CLARK LEAH 07:55:45.000 19999 2021-04-07 11:38:46.000 CLARK LEAH 11:38:46.000 19999 2021-04-07 12:10:14.000 CLARK LEAH 12:10:14.000 19999 2021-04-07 16:30:03.000 CLARK LEAH 16:30:03.000 19999 2021-04-08 07:50:41.000 CLARK LEAH 07:50:41.000 19999 2021-04-08 16:34:03.000 CLARK LEAH 16:34:03.000 19999 2021-04-09 07:18:18.000 CLARK LEAH 07:18:18.000

I convert it 100 minute time also.

### Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

#### rpaulson

##### Well-known Member
VBA Code:
``````Sub do_it()

lr = Cells(Rows.Count, "A").End(xlUp).Row

For r = 1 To lr
Cells(r, "F") = Split(Cells(r, "B"), ".")(0) 'this will write the date into column F
Next r

For r = 1 To lr Step 2
If Cells(r, "G") = Cells(r + 1, "G") Then Cells(r, "G") = (Cells(r + 1, "E") - Cells(r, "E")) * 24 'this will write the hours into column G
Next r

End Sub``````

#### JoeyGaspard

##### Board Regular
VBA Code:
``````Sub do_it()

lr = Cells(Rows.Count, "A").End(xlUp).Row

For r = 1 To lr
Cells(r, "F") = Split(Cells(r, "B"), ".")(0) 'this will write the date into column F
Next r

For r = 1 To lr Step 2
If Cells(r, "G") = Cells(r + 1, "G") Then Cells(r, "G") = (Cells(r + 1, "E") - Cells(r, "E")) * 24 'this will write the hours into column G
Next r

End Sub``````
Thank you, but I am confused, does this add/subtract the time?

#### JoeyGaspard

##### Board Regular
VBA Code:
``````Sub do_it()

lr = Cells(Rows.Count, "A").End(xlUp).Row

For r = 1 To lr
Cells(r, "F") = Split(Cells(r, "B"), ".")(0) 'this will write the date into column F
Next r

For r = 1 To lr Step 2
If Cells(r, "G") = Cells(r + 1, "G") Then Cells(r, "G") = (Cells(r + 1, "E") - Cells(r, "E")) * 24 'this will write the hours into column G
Next r

End Sub``````
This is the results I get?

#### Toadstool

##### Well-known Member

ADVERTISEMENT

Hi JoeyGaspard,

Some questions:
1. I'll be using Excel 2016. If you update your Profile with your version then others may offer better solutions.
2. The sample is for one Employee. May the list contain multiple Employees? If so then is the "19999" the Employee Number?
3. Could it be that there are 3+ pairs of clockin/clockouts?
4. Will the data always be in Clockin/Out Date and Time sequence?
5. I don't understand "I convert it 100 minute time also" so could you explain further?

#### rpaulson

##### Well-known Member
Joey,
change
Cells(r, "F") = Split(Cells(r, "B"), ".")(0) 'this will write the date into column F

to

Cells(r, "F") = Split(Cells(r, "B"), " ")(0) 'this will write the date into column F

#### JoeyGaspard

##### Board Regular

ADVERTISEMENT

Hi JoeyGaspard,

Some questions:
1. I'll be using Excel 2016. If you update your Profile with your version then others may offer better solutions.
2. The sample is for one Employee. May the list contain multiple Employees? If so then is the "19999" the Employee Number?
3. Could it be that there are 3+ pairs of clockin/clockouts?
4. Will the data always be in Clockin/Out Date and Time sequence?
5. I don't understand "I convert it 100 minute time also" so could you explain further?
q1: I am using 2016 as well
q2: Thre are multiple employees, but each one has a different employee number (yes, the 19999 in the sample is the emp #)
q3: No, there will either be 2 or 4
q4: Yes, it will always be in that format
q5: We use the "100 Minute" or decimal time scale, for example, 15:05 would be converted to 15.08, 8:58 would be converted to 8.97.

#### JoeyGaspard

##### Board Regular
Joey,
change
Cells(r, "F") = Split(Cells(r, "B"), ".")(0) 'this will write the date into column F

to

Cells(r, "F") = Split(Cells(r, "B"), " ")(0) 'this will write the date into column F
Here are the results:

Here is the vba I have:

Sub do_it()

lr = Cells(Rows.Count, "A").End(xlUp).Row

For r = 1 To lr
Cells(r, "F") = Split(Cells(r, "B"), " ")(0) 'this will write the date into column F
Next r

For r = 1 To lr Step 2
If Cells(r, "G") = Cells(r + 1, "G") Then Cells(r, "G") = (Cells(r + 1, "E") - Cells(r, "E")) * 24 'this will write the hours into column G
Next r

End Sub

#### rpaulson

##### Well-known Member
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

#### JoeyGaspard

##### Board Regular
what happened to the time in Column "E" you had it there in your first example?

if its not there, than add the second lien 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 apologize! Here is what happens when the time is in column E

And this is what debug shows:

Replies
3
Views
69
Replies
1
Views
316
Replies
0
Views
267
Replies
11
Views
18K

Threads
1,129,551
Messages
5,636,966
Members
416,953
Latest member
broexc

### 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

### 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