2013-03-02 52 views
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作为结果。使用SUMIFS添加持续时间始终给出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 

其中包含最新的源列是一般FORMATT 的我是新来的VBA宏

+0

任何想法在上面的代码中有什么错误? – amar 2013-03-02 06:54:04

+1

对于你的“慢”解决方案,你不应该循环列中的所有单元格 - 看着'UsedRange'属性。 – 2013-03-02 09:14:32

回答

3

更新的解决方案:

经与OP讨论后,决定纯配方解决方案是好的 - 下面是在单独片材起始A1

  1. 行A将被生成的表头:在A1我加入Agent Name/Release Code,和起始B1有(容易得到使用Remove Duplicates)所有可用Release Code值的列表。
  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任意数量的小区是不小的独特名称的数量 - 为我所用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 - 这将定义一个多节ARRAY公式,并会导致在大括号{}围绕它(但不要手动输入!)。
  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")

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

尝试删除第一个将零分配给cellDate1的位置。

+0

不是案件无效 – amar 2013-03-02 08:27:07

+0

你是否删除了那一个? http://floomby.ru/s1/jamrrt – 2013-03-02 09:12:45

+0

我评论说,一个。如果你看到我的第二个代码,当我使用timeValue()并添加使用循环它的作品。是我的来源有不同的格式?实际上,我必须总结一天中员工的总休息时间,因此我必须在列“F”中为列A中的特定名称添加持续时间,并在列“G”中添加特定中断代码 – amar 2013-03-02 09:16:27