使用SUMIFS添加持续时间总是给出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
我正在使用上面的代码添加时间基于其他两列的值,但我总是得到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
包含date的源列是通用格式我是VBAmacros的新手
更新的解决scheme:
在与OP交谈后,决定纯配方解决scheme是好的 – 下面是在单独的工作表上开始的公式/行动开始A1
:
- A行将产生表头:在
A1
我添加了Agent Name / Release Code
,并且从B1
开始,列出了所有可用的Release Code
值(使用Remove Duplicates
轻松获得)。 - 我定义了以下命名的范围,简单和有效(因为初始数据不是静态的):
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
范围。 - 在列
A
我们应该获得名称列表,这些名称应该是唯一的,所以在列A
select不less于唯一名称数目的单元格数目 – 对于我使用的示例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))))),"")
,然后按CTRL + SHIFT + ENTER而不是通常的ENTER – 这将定义一个Multicell ARRAY公式,并将导致在它周围的花括号{}
手动键入!)。 -
B2
:=IF(OR($A2="",SUMPRODUCT(--($A2=AgentNames),--(B$1=ReleaseCodes),TIMEVALUE(TimeInStateData))=0),"",SUMPRODUCT(--($A2=AgentNames),--(B$1=ReleaseCodes),TIMEVALUE(TimeInStateData)))
– 普通公式,它将为空名称或零时间返回空值。 - 从
B2
复制公式到整个表格。
备注:
- 时间值总和的结果范围应格式化为“
Time
。 - 如果将来要扩展名称列表,请重复步骤3 ,但不要拖动公式 – 这将导致
You cannot change part of an array
错误的You cannot change part of an array
。
示例文件: https : //www.dropbox.com/s/quudyx1v2fup6sh/AgentsTimeSUM.xls
初步答复:
也许这太简单明了了,但一眼看不明白为什么你有这样的代码行:
cellDate1 = TimeValue("00:00:00")
在您的SUMIFS
: cellDate1 = WorksheetFunction.SumIfs(aa.Range("F:F"), ...
尝试删除第一个分配给cellDate1
零。