2014-04-10 5 views
0

저는 카메라 앞에 활동이있는 한 몇 초마다 모션에 민감한 카메라로 촬영 한 동물의 존재 시간을 계산하려고합니다. 우리는 동물이 6 분 동안 존재하지 않는다면 동물의 새로운 발생 및 새로운 데이터 포인트라고 생각합니다.Excel에서 다른 셀의 값을 기준으로 두 개의 셀을 뺍니다.

동물이 카메라 앞에있는 시간을 계산하는 매크로를 만들려고합니다. 다음은 * 별 2 개를 사용하여 값을 빼기 원하는 예제입니다. 나는 읽기의 용이성을 위해 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 that adds and subtracts values based on values in other cells는 아마도 가장 가까운 this one on copying based on other cell values, this one that sums certain rows if conditions are met을 포함하여 몇 가지 예에서 근무하고했습니다. 나는 다음의 매크로 초안을 만들었지 만, 나는 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

단지에 대한 명확한 설명을 원하면 매크로가 'Dur of visit'열을 계산하도록할까요? 실제 데이터는 첫 번째로 제거되거나'예를 들어 순전히 그림에 불과한가? ' –

+0

깨끗한 3 열 또는 4 열 데이터 세트로 시작하고 있습니까? – 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 : 이것은 아주 좋습니다! 정말 고마워! –

관련 문제