在Excel中,根据另一个单元格中的值减去两个单元格

我试图计算dynamic相机每隔几秒拍摄的动物的存在时间,只要在相机前面有活动。 我们假设如果动物在6分钟内不存在,我们认为它是一个新的动物和新的数据点。

我正在试图创build一个macros来计算摄像机前动物的持续时间。 这里是一个例子,我想用两个星**中的数值减去* 1星*以上的数值。 为了便于阅读,我将#散列标记#放在大于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 

我已经从几个例子,包括这个基于其他单元格值的复制 , 这是一个总和某些行,如果条件符合 ,可能是最接近的, 这一个基于其他单元格值加减值 。 我开发了以下的macros,但我是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 

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

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

这是我的结果: 在这里输入图像说明

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