Calculate average time excluding outliers

Deepk

Board Regular
Joined
Mar 21, 2018
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I have the following time spent data. I want a VBA code that can give the average time spent, total time spent, and number of days in the last columns (variable) against each employee. The time entries less than 4:30 and greater than 13:00 hours should not be included during the average calculation.

I have done calculations for two rows to show the desired output.

Looking forward in anticipation!

Attendance_Jan 20221(AutoRecovered).xlsx
CDEFGHIJK
4Emp NameMon 03Tue 04Wed 05Thu 06Fri 07AverageTotal time spentNumber of days
5ABC19:299:038:388:509:349:0645:34:005
6ABC28:309:299:017:36
7ABC39:089:3410:009:289:30
8ABC49:049:279:008:21
9ABC58:146:514:35
10ABC69:206:4910:149:049:00
11ABC78:459:119:149:3911:59
12ABC89:118:589:109:338:51
13ABC99:1811:2014:259:049:19
14ABC109:463:159:124:255:248:0724:22:003
15ABC119:1510:349:089:166:44
16ABC129:198:459:29
17ABC138:599:1023:299:0610:16
18ABC1413:3412:123:1510:028:50
19ABC159:149:339:429:1111:32
20ABC169:039:109:583:158:55
21ABC178:199:099:368:55
22ABC189:109:269:389:199:30
Sheet9
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello Deepk,

This is not VBA. It is just formulas that will do what you want. I used numbers 1 - 5 in column D-H.

then in Cell I5 (Copy & PAste) =(SUMIF(D5:H5,">1")-SUMIF(D5:H5,">4"))/(COUNT(D5:H5)-(COUNTIF(D5:H5,">4")+COUNTIF(D5:H5,"<2"))) (Mean Average)
then in Cell J5 (Copy & PAste) =(SUMIF(D5:H5,">1")-SUMIF(D5:H5,">4")) (Total)
then in Cell J5 =J5/24 (Hours to Days)

* Obviously change the ">1" parts to fit your data.
Copy the formulas down to the other cells.

No code needed.

Hope this is good for you; with no VBA.

Jamie
 
Upvote 0
Hello Deepk,

This is not VBA. It is just formulas that will do what you want. I used numbers 1 - 5 in column D-H.

then in Cell I5 (Copy & PAste) =(SUMIF(D5:H5,">1")-SUMIF(D5:H5,">4"))/(COUNT(D5:H5)-(COUNTIF(D5:H5,">4")+COUNTIF(D5:H5,"<2"))) (Mean Average)
then in Cell J5 (Copy & PAste) =(SUMIF(D5:H5,">1")-SUMIF(D5:H5,">4")) (Total)
then in Cell J5 =J5/24 (Hours to Days)

* Obviously change the ">1" parts to fit your data.
Copy the formulas down to the other cells.

No code needed.

Hope this is good for you; with no VBA.

Jamie
hi Jamie,

Thank you for your reply.

Please note that the member who is going to use this sheet is not very friendly with excel and formulas. To simplify the process and we would need a vba code. Please help here. Thank you.
 
Upvote 0
Hello Deepk,

The code below is set for the first piece of data in D5: as per your image.

* Obviously change the ">7" "<4"parts to fit your data.

VBA Code:
Sub TimeSpent()
Dim i As Integer, j As Integer
i = 1
j = 1
'Loops from row 5 to 15 (Change to the last row of your Data
For i = 5 To 15
' Change the <> values to suit 'Finds the total Hours
Cells(i, 10) = Cells(i, 4) + Cells(i, 5) + Cells(i, 6) + Cells(i, 7) + Cells(i, 8) - Application.WorksheetFunction.SumIf(Range("D5:H5").Offset(j - 1), ">7") - Application.WorksheetFunction.SumIf(Range("D5:H5").Offset(j - 1), "<4")
' Change the <> values to suit 'Finds the total Days
Cells(i, 11) = 5 - (Application.WorksheetFunction.CountIf(Range("D5:H5").Offset(j - 1), ">7") + Application.WorksheetFunction.CountIf(Range("D5:H5").Offset(j - 1), "<4"))
' Eliminates errors if zero hours - dividing by 0 ' Gives the Mean Average
If Cells(i, 11) > 0 Then
Cells(i, 9) = Cells(i, 10) / Cells(i, 11)
Else
Cells(i, 9).Value = 0
End If
j = j + 1
Next i
End Sub

Jamie
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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