VBA average with criteria on time formatted cells

RFLundgren

New Member
Joined
Mar 28, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi all

I have a sheet that contains multiple columns, with one being time based and another showing status.

It has to loop through the data as the number of rows will change each month. Columns remain static.

I need to get the average of the time based cells in the column where status = Closed.

So far in my searching I have come across the code below, which works for the most part except for the error 6 overflow I am getting.

I have tried declaring as an integer and as a string, however get the same error in either case.

Example sheet
1648518250289.png


Example code
1648518292413.png


Once I have the average, I then need to divide it by 8 and place it into a cell on a different sheet.

Any help / suggestions, or even whole new code, would be most appreciated.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I guess you were running code when active sheet was "Sheet1" (sheet result) (data is in other sheet, i.e, sheet2)
So, try to add sheet name within code, like this:
VBA Code:
Option Explicit
Sub test()
Dim Lr&, sum As Double, cell As Range
With Worksheets("Sheet2") 'working with sheet that contains data
Lr = .Cells(Rows.count, 3).End(xlUp).Row ' define last row of column C
    For Each cell In .Range("C2:C" & Lr) ' loop through each cell in column C
        If cell.Value Like "Closed" Then
            sum = sum + cell.Offset(0, 14).Value ' running sum of time stamp in column Q
        End If
    Next
        With Worksheets("Sheet1").Range("D8") ' paste value and format into cell D8 other sheet
            .Value = sum / (Lr - 1)
            .NumberFormat = "[hh]:mm:ss"
        End With
End With
End Sub
With data is in sheet2, result is in sheet1
Book2.xlsx
CQ
1Statushrs
2Closed103:24:55
3Change Request00:00
4Closed147:41:28
5Closed13:26:35
6Defect tracker00:00
7Closed259:40:48
8Closed00:53
9Closed15:03:17
Sheet2

Book2.xlsx
D
867:31:15
Sheet1
 
Upvote 0
Solution

Forum statistics

Threads
1,214,614
Messages
6,120,517
Members
448,968
Latest member
Ajax40

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