使用vbscript添加时间值

时间:2012-12-13 19:26:21

标签: excel-vba vbscript vba excel

我可以知道有没有办法直接添加两个或更多的持续时间?还有一件事:它们位于已定义或评估并存储在这些列中的每一行的Excel列中。它们以存储的Excel列中的dd:hh:mm格式显示。没有为每一行添加列的库存,是否有任何技术可以通过搜索其格式直接添加它们并将其作为总和放入另一列?

10:23:10(dd:hh:mm)。

更新

PID     T1Nm             T1SD                   T1FD                T1TotTime     T2Nm        T2SD                     T2FD              T2TotTime      TotalTaskDuration

 10      T1      9/27/2012  12:53:03 PM   9/3/2012  1:52:20 PM       23:23:0      T2   9/26/2012  5:55:32 PM    9/14/2012  1:52:20 PM      12:4:3

 20      T1      9/6/2012  8:29:34 AM     9/17/2012  8:59:36 AM      11:0:30      T4   9/26/2012  5:55:32 PM    9/14/2012  1:52:20 PM      12:4:3

在这里,我只是尝试给你一个图解视图,以便你可以理解我在寻找什么。每列中都有不同的时间格式。所以我们只需要捕获dd:hh:mm格式并添加它们并将其存储到最后一列。希望这个描述可以帮助你理解整个事情我我正在寻找。

编辑: 获得所有列的总时间计算是相同的。请建议修复。 意外输出

enter image description here

更新

格式化时仍然出错:

enter image description here

感谢,

1 个答案:

答案 0 :(得分:1)

电子表格/代码示例:http://www.bumpclub.ee/~jyri_r/Excel/Summing_time_differences.xls

Sub CalculateTotalTimes()

  Dim rng As Range
  Dim column_counter As Long
  Dim row_counter As Long
  Dim time_sum() As Variant
  Dim dest_rng As Range
  Dim x As Variant

 Set rng = ActiveSheet.UsedRange

 ReDim time_sum(rng.Rows.Count)

     For row_counter = 1 To rng.Rows.Count
         time_sum(row_counter - 1) = 0
           For column_counter = 1 To rng.Columns.Count - 1 'TotalTaskDuration is the last column, not counted
              If rng(row_counter, column_counter).NumberFormat = "dd:hh:mm" Then
                time_sum(row_counter - 1) = time_sum(row_counter - 1) + rng(row_counter, column_counter).Value2
              End If
           Next
     Next
       x = LBound(time_sum)
         For x = LBound(time_sum) To UBound(time_sum) - 1
            time_sum(x) = time_sum(x + 1)
         Next

   ReDim Preserve time_sum(UBound(time_sum) - 1)

   Set dest_rng = ActiveSheet.Cells(2, rng.Columns.Count)
    Set dest_rng = dest_rng.Resize(UBound(time_sum), 1)
  dest_rng.Value = Application.WorksheetFunction.Transpose(time_sum)

End Sub