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宏的新手
答案 0 :(得分:3)
更新的解决方案:
在与OP聊天后讨论确定纯配方解决方案很好 - 下面是从A1
开始的单独表格中要执行的公式/操作:
A1
中我添加了Agent Name / Release Code
,并且从B1
开始,列出了所有可用的Release Code
值(使用{{很容易获得) 1}})。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
的示例,并键入该公式:A2:A51
并按 CTRL + SHIFT + ENTER 而不是通常的 ENTER - 这将定义一个 Multicell ARRAY 公式,并在其周围产生卷曲=IFERROR(INDEX(AgentNames,SMALL(IF(MATCH(AgentNames,AgentNames,0)=ROW(INDIRECT("1:"&ROWS(AgentNames))),MATCH(AgentNames,AgentNames,0),""),ROW(INDIRECT("1:"&ROWS(AgentNames))))),"")
括号(但是 NOT 手动输入!)。{}
:B2
- 正常公式,将为空名称或零时间返回空值。=IF(OR($A2="",SUMPRODUCT(--($A2=AgentNames),--(B$1=ReleaseCodes),TIMEVALUE(TimeInStateData))=0),"",SUMPRODUCT(--($A2=AgentNames),--(B$1=ReleaseCodes),TIMEVALUE(TimeInStateData)))
复制到整个表格。说明:
B2
。Time
错误。< / LI>
示例文件:https://www.dropbox.com/s/quudyx1v2fup6sh/AgentsTimeSUM.xls
INITIAL ANSWER:
也许这太简单明了,但一眼就看出我不明白为什么你会有这样的代码:
You cannot change part of an array
在cellDate1 = TimeValue("00:00:00")
:SUMIFS
尝试删除您为cellDate1 = WorksheetFunction.SumIfs(aa.Range("F:F"), ...
分配零的第一个。