1
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

我使用上面的代码根据其他两列的值添加持续时间,但我总是得到 00:00:00 作为结果。

如果我使用下面的代码,我会得到答案,但它太慢非常慢

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

包含日期的源列是一般格式我是 VBA 宏的新手

4

1 回答 1

3

更新的解决方案:

在与 OP 聊天讨论后,决定纯公式解决方案很好 - 以下是要在单独的工作表上执行的公式/操作A1

  1. A 行将生成表头:在A1I addedAgent Name / Release Code中,开始B1有一个所有可用Release Code值的列表(很容易使用Remove Duplicates)。
  2. 为了简单和有效,我定义了以下命名范围(因为初始数据不是静态的):AgentNames=OFFSET('Agent State'!$B$2,0,0,COUNTA('Agent State'!$B:$B)-1,1)- 这将返回初始工作表上不包括标题的名称范围;TimeInStateData=OFFSET(AgentNames,0,4)ReleaseCodes=OFFSET(AgentNames,0,5)作为移位AgentNames范围。
  3. 在列A中,我们应该获得应该是唯一的名称列表,因此在列中选择不小于唯一名称数量的A任意数量的单元格- 对于我使用的示例,然后输入该公式:然后按+ +而不是通常- 这将定义一个Multicell ARRAY公式并在其周围产生花括号(但不要手动输入它们!)。A2:A51=IFERROR(INDEX(AgentNames,SMALL(IF(MATCH(AgentNames,AgentNames,0)=ROW(INDIRECT("1:"&ROWS(AgentNames))),MATCH(AgentNames,AgentNames,0),""),ROW(INDIRECT("1:"&ROWS(AgentNames))))),"")CTRLSHIFTENTERENTER{}
  4. B2: =IF(OR($A2="",SUMPRODUCT(--($A2=AgentNames),--(B$1=ReleaseCodes),TIMEVALUE(TimeInStateData))=0),"",SUMPRODUCT(--($A2=AgentNames),--(B$1=ReleaseCodes),TIMEVALUE(TimeInStateData)))- 普通公式,它将为空名称或零时间返回空值。
  5. 将公式从复制B2到整个表格。

评论:

  • 时间值总和的结果范围应格式化为Time.
  • 如果将来要扩展名称列表 -对新范围重复步骤 3,但不要向下拖动公式 - 这将导致You cannot change part of an array错误。

示例文件:https ://www.dropbox.com/s/quudyx1v2fup6sh/AgentsTimeSUM.xls

初步答案:

也许这太简单明了,但乍一看我不明白为什么你有那行代码:

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

在你之后SUMIFScellDate1 = WorksheetFunction.SumIfs(aa.Range("F:F"), ...

尝试删除您将零分配给的第一个cellDate1

于 2013-03-02T07:32:54.113 回答