Sum Column Differences between date range

Kellogg

New Member
Joined
Mar 30, 2013
Messages
41
Platform
  1. Windows
  2. MacOS
Code:
Dear Great Minds of Excel,


I have the following data (Except column D)

Column A     Column B        Column C       Column D     Column E     Column F      Column G
Date 1         Start Time 1    End Time 2     Difference    Start Date    End Date       Avg Time to Complete
01/01/14     08:00              08:30             00:30          2/1/14         2/28/14        00:25
01/0714       09:00             09:10              00:10
02/04/14     10:00              10:18              00:18
02/22/14     11:00              11:02              00:02
02/25/14     12:00              12:30              00:30


I want a vba code that will calculate the time difference between Column B and Column C (shown in column D) then calculate the average time to complete tasks over a date range designated in Cells E1 and F1.

Cell E1 = 02/01/14 This is the start of the date range.
Cell F1 = 02/28/14 This is the end of the date range.
Cell G1 = Average time to complete task within date range.

I do not need to have the differences listed in column D.  This is for clarification. The answer should be 00:18 + 00:02 + 00:30 / 3 = 00:25. 

Something using:
Application.Worksheetfunction.sumifs(DateRange1, ......

Any help is always appreciated.  Thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi

I'm confused - shouldn't the result be 00:16:40 (16 minutes and 40 seconds)?

00:18 + 00:02 + 00:30 = 00:50 that divided by 3 results in 00:16:40

Could you clarify?

M.
 
Upvote 0
Marcelo,

You are right. I was having a brain freeze when I was typing.
 
Upvote 0
Maybe something like this

Code:
Dim lastRow As Long


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


With Range("G2")
    .Value = Evaluate(Replace("=SUMPRODUCT(C2:C@-B2:B@,--(A2:A@>=E2),--(A2:A@<=F2))/3", "@", lastRow))
    .NumberFormat = "hh:mm:ss"
End With

Hope this helps

M.
 
Upvote 0
Remark
The formula above assumes data beginning in row 2, headers in row 1
So E2= 2/1/14 and F2=2/28/14

M.
 
Upvote 0
Marcelo,

The number of entries will continue to change as the new are appended for each month to the bottom of the list. The number three will have to change to equal the number of entries between the date range. So if E2 = 1/1/14 and F2 = 1/31/14 3 would be 2. Does this make sense?
 
Upvote 0
Marcelo,

The number of entries will continue to change as the new are appended for each month to the bottom of the list. The number three will have to change to equal the number of entries between the date range. So if E2 = 1/1/14 and F2 = 1/31/14 3 would be 2. Does this make sense?

Yes, you are right. Wait for a new formula.

M.
 
Upvote 0
Try

Code:
Dim lastRow As Long, numDates As Long


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


numDates = Evaluate(Replace("=Countifs(A2:A@,"">=""&E2,A2:A@,""<=""&F2)", "@", lastRow))


With Range("G2")
    .Value = Evaluate(Replace("=SUMPRODUCT(C2:C@-B2:B@,--(A2:A@>=E2),--(A2:A@<=F2))", "@", lastRow)) / numDates
    .NumberFormat = "hh:mm:ss"
End With

M.
 
Upvote 0
Code:
Column A  Column B      Column C    Column D    Column E    Column F  Column G
Date 1    Start Time 1  End Time 2  Difference  Start Date  End Date  Avg Time to Complete
01/01/14  08:00         08:30       00:30       2/1/14      2/28/14   00:25
01/07/14  09:00         09:10       00:10
02/04/14  10:00         10:18       00:18
02/22/14  11:00         11:02       00:02
02/25/14  12:00         12:30       00:30
I want a vba code that will calculate the time difference between Column B and Column C (shown in column D) then calculate the average time to complete tasks over a date range designated in Cells E1 and F1.

Code:
Sub doit()
Dim dates As Range, startTimes As Range, endTimes As Range
Dim startDate As String, endDate As String
Set dates = Range("a1", Cells(1, 1).End(xlDown))
Set startTimes = dates.Offset(0, 1)
Set endTimes = dates.Offset(0, 2)
startDate = ">=" & Range("e1")
endDate = "<=" & Range("f1")
With Range("g1")
    .NumberFormat = "[h]:mm:ss.000"
    .Value = (WorksheetFunction.SumIfs(endTimes, dates, startDate, dates, endDate) _
        - WorksheetFunction.SumIfs(startTimes, dates, startDate, dates, endDate)) _
        / WorksheetFunction.CountIfs(dates, startDate, dates, endDate)
End With
End Sub
 
Upvote 0
Marcelo,

This works great. Thanks. This is like Christmas came early. I will use this in many worksheets.
 
Upvote 0

Forum statistics

Threads
1,216,473
Messages
6,130,838
Members
449,597
Latest member
buikhanhsang

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