2014-04-10 240 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! –

相關問題