基于Excel中另一个单元格中的值减去两个单元格

时间:2014-04-10 18:04:38

标签: excel vba excel-vba

只要相机前面有活动,我就会尝试每隔几秒计算动作敏感相机拍摄动物的存在时间。我们假设如果动物不存在6分钟,我们认为它是动物和新数据点的新发生。

我正在尝试创建一个宏来计算动物在相机前面的持续时间。这是一个例子,我想用**两个星**中的值减去几个记录的值,用*一个星*更高。为了便于阅读,我将#hash-marks#放在大于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

提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:3)

它没有使用VBA,但可以采用公式并将其插入到VBA代码中。

这是我的结果: enter image description here

A,B,C列是数据。

Cell D2:

=B2+C2

细胞E2:

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

Cell F2:

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

Cell 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)))