2014-04-10 161 views
0

我试图计算动态相机每隔几秒拍摄动物的持续时间,只要在摄像机前有活动即可。我们假设如果动物在6分钟内不存在,我们认为它是一种新的动物和新的数据点。基于Excel中另一个单元格中的值减去两个单元格

我正试图创建一个宏来计算动物出现在镜头前的持续时间。这里是一个例子,我想用*一星*的几个记录中的数值减去**两星**中的值。为了便于阅读,我将#散列标记#放在大于6分钟的“时间差”值周围。

Photo # Date  Time  Difference of time  Duration of visit 
    1 2/9/2012 *8:01:30 PM*  NA      0:08:48      
    2 2/9/2012 8:01:31 PM  0:00:01   
    3 2/9/2012 8:01:36 PM  0:00:05   
    4 2/9/2012 8:01:54 PM  0:00:18   
    5 2/9/2012 8:02:36 PM  0:00:42   
    6 2/9/2012 8:02:48 PM  0:00:12   
    7 2/9/2012 8:03:07 PM  0:00:19   
    8 2/9/2012 8:03:23 PM  0:00:16   
    9 2/9/2012 8:04:18 PM  0:00:55   
    10 2/9/2012 8:04:42 PM  0:00:24   
    11 2/9/2012 8:05:02 PM  0:00:20   
    12 2/9/2012 8:05:52 PM  0:00:50   
    13 2/9/2012 8:07:08 PM  0:01:16   
    14 2/9/2012 8:08:59 PM  0:01:51   
    15 2/9/2012 8:10:09 PM  0:01:10   
    16 2/9/2012 **8:10:18 PM** 0:00:09   
    17 2/9/2012 *8:18:08 PM* #0:07:50#    0:01:22  
    18 2/9/2012 8:18:11 PM  0:00:03   
    19 2/9/2012 8:18:23 PM  0:00:12   
    20 2/9/2012 8:18:34 PM  0:00:11   
    21 2/9/2012 **8:19:30 PM** 0:00:56   
    22 2/10/2012 *8:36:51 AM* #12:17:21#    0:01:44  
    23 2/10/2012 8:38:00 AM  0:01:09   
    24 2/10/2012 8:38:06 AM  0:00:06   
    25 2/10/2012 8:38:08 AM  0:00:02   
    26 2/10/2012 8:38:17 AM  0:00:09   
    27 2/10/2012 8:38:33 AM  0:00:16   
    28 2/10/2012 **8:38:35 AM** 0:00:02   
    29 2/12/2012 9:15:00 AM  #0:36:25#      X  

我从几个例子,包括this one on copying based on other cell valuesthis one that sums certain rows if conditions are met工作,并可能最接近,this one that adds and subtracts values based on values in other cells。我开发了下面的草稿宏,但我是VBA的新手,无法将它们合在一起。

Sub CalculateDuration() 

Dim duration As Date 

'I basically want this to mean search down row D (Difference of time column) for values that are greater than 6 minutes 

duration = Cells(Rows.Count, "D").End(xlUp).Row 
    For Each cell In Range("D:D") 
     If cell.Value >= Range("G2").Value Then 

'Cell G2 has the 6 minute value in it (i did it this way since it's set as a time value) although I could also add a 00:06:00 if that would work. 

       ActiveCell.Offset(0, 1).Value = X - ActiveCell.Offset(0, -1).Value 

       'I need to point the value of X to be the next value in column C that is just one row higher than the next value in Column D that's >= 0:06:00 minutes. I'm not sure the best way to go about that... perhaps a nested if/then? 

'I also think I may need something to make this write or end properly... 

       End If 
    Next 

End Sub 

在此先感谢您提供的任何帮助。

+0

只是为了澄清,你想要宏来计算'访问'列'?真实的数据是否有*和s#要先被删除,或者这些纯粹是为了举例说明? –

+0

因此,您从一个干净的三列或四列数据集开始? – Ken

+0

@DeanMacGrego,是的,我需要计算访问持续时间列。真实的数据没有* s或#。他们只是为了说明我需要计算的单元格。 –

回答

3

它不使用VBA,但公式可以被采用并插入到VBA代码中。

这是我的结果: enter image description here

列A,B,C是数据。

单元格D2:

=B2+C2 

细胞E2:

=IF(ROW(D2)=2,0,D2-D1) 

小区F2:

=IF(ROW(A2)=2,1,IF(E2<6/60/24,F1,F1+1)) 

细胞G2:

=IF(F2=F1,"",INDEX($D$2:$D$30,MATCH(F2+1,$F$2:$F$30,0)-1)-INDEX($D$2:$D$30,MATCH(F2,$F$2:$F$30,0))) 
+0

+1:我非常喜欢这种方法,更清洁和可扩展。 – Manhattan

+0

+1:这太好了!非常感谢Ken! –

相关问题