amar
amar

Reputation: 4343

Using SUMIFS to add time duration always gives 00:00:00

Sub Add_sumf()
Dim i As Integer
i = 3
Dim cellDate As Integer
cellDate = 0
Dim cellDate1 As Date
cellDate1 = TimeValue("00:00:00")
Dim total    As Integer
total = 0
Dim j As Integer
j = 2
Dim k As Integer
k = 2
Set aa = Workbooks("Book3").Worksheets(1)
Set bb = Workbooks("Final_result").Worksheets(1)
Do While bb.Cells(1, k).Value <> ""

    For Each y In bb.Range("A:A")
    On Error GoTo Label

    If UCase(bb.Cells(j, "A").Value) <> "" Then


     cellDate1 = WorksheetFunction.SumIfs(aa.Range("F:F"), aa.Range("B:B"), UCase(bb.Cells(1, k).Value), aa.Range("G:G"), UCase(bb.Cells(j, "A").Value))                         
    bb.Cells(j, k).Value = TimeValue(cellDate1)

    cellDate1 = TimeValue("00:00:00")
    bb.Cells(j, k).NumberFormat = "[h]:mm:ss"

    On Error GoTo Label

    j = j + 1
    Else
    Exit For
    End If
    Next
    j = 2
    k = k + 1
 Loop
Label:
    'MsgBox Err.Description
    Exit Sub


End Sub

I am using above code to add time duration based upon value of two other columns but I always get 00:00:00 as result.

if i use below code i get the answer but its too slow very slow

Sub add_it_time()
Dim i As Integer
i = 3
Dim cellDate As Integer
cellDate = 0
Dim cellDate1 As Date
cellDate1 = TimeValue("00:00:00")
Dim total    As Integer
total = 0
Dim j As Integer
j = 2
Dim k As Integer
k = 2
Set aa = Workbooks("Book3").Worksheets(1)
Set bb = Workbooks("Final_result").Worksheets(1)
Do While bb.Cells(1, k).Value <> ""
 'MsgBox bb.Cells(1, k).Value
    For Each y In bb.Range("A:A")
    On Error GoTo Label
   ' MsgBox UCase(bb.Cells(j, "A").Value)
    If UCase(bb.Cells(j, "A").Value) <> "" Then

        For Each x In aa.Range("F:F")
        On Error Resume Next
        If UCase(aa.Cells(i, "B").Value) = UCase(bb.Cells(j, "A").Value) Then
       ' MsgBox aa.Cells(i, "F").Text
       ' total = total + Int(get_Second(aa.Cells(i, "F").Text))
        If UCase(aa.Cells(i, "G").Value) = UCase(bb.Cells(1, k).Value) Then
         'MsgBox aa.Cells(i, "F").Text
        cellDate1 = cellDate1 + TimeValue(aa.Cells(i, "F").Value)
        End If
        End If
        i = i + 1
        Next
        i = 3
        On Error GoTo Label
         bb.Cells(j, k).NumberFormat = "h:mm:ss"
        bb.Cells(j, k).Value = WorksheetFunction.Text(cellDate1, "[hh]:mm:ss")
        total = 0
        cellDate1 = 0
    j = j + 1
    Else
    Exit For
    End If
    Next
    j = 2
    k = k + 1
 Loop
Label:
    'MsgBox Err.Description
    Exit Sub
End Sub

The source column which contains date is of general formatt I am new to VBA macros

Upvotes: 1

Views: 600

Answers (1)

Peter L.
Peter L.

Reputation: 7304

UPDATED SOLUTION:

After discussion in chat with OP it was decided that pure formula solution is fine - below are formulas / actions to do on the separate sheet starting A1:

  1. Row A will be resulting table header: in A1 I added Agent Name / Release Code, and starting B1 there's a list of all available Release Code values (easily got using Remove Duplicates).
  2. I defined the following named ranges for the simplicity and effectiveness (since initial data is NOT static): AgentNames=OFFSET('Agent State'!$B$2,0,0,COUNTA('Agent State'!$B:$B)-1,1) - this will return the range of names on the initial sheet excluding the header; TimeInStateData=OFFSET(AgentNames,0,4) and ReleaseCodes=OFFSET(AgentNames,0,5) as shifted AgentNames range.
  3. In column A we should obtain the list of names, which should be unique, so select in column A any number of cells which is NOT less that number of unique names - for the sample I used A2:A51, and type that formula: =IFERROR(INDEX(AgentNames,SMALL(IF(MATCH(AgentNames,AgentNames,0)=ROW(INDIRECT("1:"&ROWS(AgentNames))),MATCH(AgentNames,AgentNames,0),""),ROW(INDIRECT("1:"&ROWS(AgentNames))))),"") and press CTRL+SHIFT+ENTER instead of usual ENTER - this will define a Multicell ARRAY formula and will result in curly {} brackets around it (but do NOT type them manually!).
  4. B2: =IF(OR($A2="",SUMPRODUCT(--($A2=AgentNames),--(B$1=ReleaseCodes),TIMEVALUE(TimeInStateData))=0),"",SUMPRODUCT(--($A2=AgentNames),--(B$1=ReleaseCodes),TIMEVALUE(TimeInStateData))) - normal formula, which will return empty value for either empty name or zero time.
  5. Copy formula from B2 to the whole table.

Remarks:

  • Resulting range for the sum of time values should be formatted as Time.
  • If the list of names should be expanded in the future - repeat step 3 for the new range, but do NOT drag the formula down - this will result in You cannot change part of an array error.

Sample file: https://www.dropbox.com/s/quudyx1v2fup6sh/AgentsTimeSUM.xls

INITIAL ANSWER:

Perhaps that's too simple and obvious, but at a glance I don't understand why you have that line of code:

cellDate1 = TimeValue("00:00:00")

right after your SUMIFS: cellDate1 = WorksheetFunction.SumIfs(aa.Range("F:F"), ...

Try to remove the first one where you assign zeros to cellDate1.

Upvotes: 3

Related Questions