Cycle Time - Macro Help

Plokimu77

Board Regular
Joined
Oct 1, 2014
Messages
138
Hello Forum,

My code below works great, however I have multiple unique PIDLabel groups in column D, separated with/by an empty row,
however my current macro stops in the first grouping.

Can some one please help me, so it can provide me the cycle time for each of the groups in column D?

Thank you

VBA Code:
Sub MCycleTime()

Dim LastRow As Long
Dim i As Long
Dim StartTime As Date
Dim EndTime As Date
Dim FoundFirst030 As Boolean
Dim OutputRow As Long


' Find the last row with data in column E
LastRow = Cells(Rows.Count, "E").End(xlUp).Row

' Initialize vari030les
FoundFirst030 = False

' Loop through the rows
For i = 2 To LastRow ' Assuming row 1 contains headers

' Check if the event code is 030
If Cells(i, "E").Value = "030" Then
StartTime = Cells(i, "N").Value
FoundFirst030 = True
End If

' Check if the event code is 100 and we've already found the first 030
If Cells(i, "E").Value = "100" And FoundFirst030 Then
EndTime = Cells(i, "N").Value
OutputRow = i
End If

Next i

' Calculate and display the cycle time
If FoundFirst030 And EndTime > 0 Then
Cells(OutputRow, "O").Value = Format(EndTime - StartTime, "hh:mm:ss")
Else

MsgBox "Cycle time measurement could not be completed."

End If

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
you are not resetting your control flags when you get tothe end of the cycle time and you had the loop control such that you only wrote out the results once, try this modification:
VBA Code:
Sub MCycleTime()

Dim LastRow As Long
Dim i As Long
Dim StartTime As Date
Dim EndTime As Date
Dim FoundFirst030 As Boolean
Dim OutputRow As Long


' Find the last row with data in column E
LastRow = Cells(Rows.Count, "E").End(xlUp).Row

' Initialize vari030les
FoundFirst030 = False

' Loop through the rows
For i = 2 To LastRow ' Assuming row 1 contains headers

' Check if the event code is 030
If Cells(i, "E").Value = "030" And Not (FoundFirst030) Then
StartTime = Cells(i, "N").Value
FoundFirst030 = True
End If

' Check if the event code is 100 and we've already found the first 030
If Cells(i, "E").Value = "100" And FoundFirst030 Then
EndTime = Cells(i, "N").Value
OutputRow = i
End If


' Calculate and display the cycle time
If FoundFirst030 And EndTime > 0 Then
Cells(OutputRow, "O").Value = Format(EndTime - StartTime, "hh:mm:ss")
FoundFirst030 = False
Else

MsgBox "Cycle time measurement could not be completed."

End If
Next i
End Sub
 
Upvote 0
Thank you.

However, now the MsgBox keeps appearing and I have to keep on clicking ok.

Is there something you can modify to help?
 
Upvote 0
It really depends on what your data includes: in each group of data is there always an "endtime" ?? if there is you can just delete that line. If there isnt you need to have another way of detecting that you have got to the end of the group, ( i.e. is there a blank cell in column D??)
 
Upvote 0
Actually each grouping is separated by an empty row.

Here is an example of my data.

TRACIDMACODEPIDLABELEVENTCODEDATETIMEZIPCODEDEVICEDEVICEIDZIPCODEKCODEFACIDTIMESTAMP
1,000,0004209011007018328762060309/29/202316:42:4876179MI15116D81DB76179801698,3229/29/2023 16:42:48.000
1,000,002420901100701832876206PL9/29/202319:56:38MH??801-19/29/2023 19:56:38.000
-1,0014209011007018328762061009/29/202319:56:3876161SP001-000002?80137,6289/29/2023 19:56:38.000
1,000,0004209011007026170541610309/20/202310:43:1876106MDC102A178677610680178,5049/20/2023 10:43:18.000
1,000,0014209011007026170541610309/28/202305:52:2576106MI14231D87537610680178,5049/28/2023 05:52:25.000
1,000,002420901100702617054161169/28/202305:57:3376106MI14231D87537610680178,5049/28/2023 05:57:33.000
1,000,005420901100702617054161PL9/29/202307:30:03MH??801-19/29/2023 07:30:03.000
-1,0014209011007026170541611009/29/202307:30:0376161SVS018702280?80137,6409/29/2023 07:30:03.000
1,000,00042090110070263902333603010/6/202306:35:2276106MI14231D87537610680178,50410/6/2023 06:35:22.000
1,000,0014209011007026390233361610/6/202306:37:4076106MI14231D87537610680178,50410/6/2023 06:37:40.000
1,000,004420901100702639023336PL10/6/202321:39:36MH??801-110/6/2023 21:39:36.000
-1,00142090110070263902333610010/6/202321:39:3676161SP001-000003?80137,62810/6/2023 21:39:36.000
1,000,0004209011007026392439250309/22/202310:16:3776240MDD051A009197624080137,6689/22/2023 10:16:37.000
1,000,002420901100702639243925PL9/22/202319:46:16MH??801-19/22/2023 19:46:16.000
-1,0014209011007026392439251009/22/202319:46:1676161SP001-000002?80137,6289/22/2023 19:46:16.000
1,000,0004209011007026478145440309/15/202313:24:2476179MDC063A009317610680178,5049/15/2023 13:24:24.000
1,000,0014209011007026478145440309/16/202311:22:0076106MI15084D85717610680178,5049/16/2023 11:22:00.000
1,000,002420901100702647814544169/16/202311:33:2376106MI15084D85717610680178,5049/16/2023 11:33:23.000
1,000,005420901100702647814544PL9/29/202307:27:50MH??801-19/29/2023 07:27:50.000
-1,0014209011007026478145441009/29/202307:27:5076161SVS018702280?80137,6409/29/2023 07:27:50.000
1,000,0004209011007026505505140309/29/202311:44:2376119PO?7611980152,9979/29/2023 11:44:23.000
-1,0024209011007026505505141009/29/202322:22:0376161AP022-1?80137,6289/29/2023 22:22:03.000
-1,0014209011007026505505141009/29/202322:27:1376161AP022-1?80137,6289/29/2023 22:27:13.000
1,000,002420901100702650550514PL9/29/202322:27:13MH??801-19/29/2023 22:27:13.000
1,000,00042090110070265083406503010/5/202313:13:0576131MDC061A014487610680178,50410/5/2023 13:13:05.000
1,000,00142090110070265083406503010/5/202313:13:1576131MDC061A014487610680178,50410/5/2023 13:13:15.000
1,000,00242090110070265083406503010/6/202306:35:0776106MI14231D87537610680178,50410/6/2023 06:35:07.000
1,000,0034209011007026508340651610/6/202306:39:2576106MI14231D87537610680178,50410/6/2023 06:39:25.000
1,000,006420901100702650834065PL10/6/202321:41:08MH??801-110/6/2023 21:41:08.000
-1,00142090110070265083406510010/6/202321:41:0876161SP001-000004?80137,62810/6/2023 21:41:08.000
 
Upvote 0
try this modification which detects the blank cell in column E and resets the logic
VBA Code:
Sub MCycleTime()

Dim LastRow As Long
Dim i As Long
Dim StartTime As Date
Dim EndTime As Date
Dim FoundFirst030 As Boolean
Dim OutputRow As Long


' Find the last row with data in column E
LastRow = Cells(Rows.Count, "E").End(xlUp).Row

' Initialize vari030les
FoundFirst030 = False

' Loop through the rows
For i = 2 To LastRow ' Assuming row 1 contains headers

' Check if the event code is 030
If Cells(i, "E").Value = "030" And Not (FoundFirst030) Then
StartTime = Cells(i, "N").Value
FoundFirst030 = True
End If

' Check if the event code is 100 and we've already found the first 030
If Cells(i, "E").Value = "100" And FoundFirst030 Then
EndTime = Cells(i, "N").Value
OutputRow = i
End If


' Calculate and display the cycle time
If FoundFirst030 And EndTime > 0 Then
Cells(OutputRow, "O").Value = Format(EndTime - StartTime, "hh:mm:ss")
FoundFirst030 = False
End If

' reset for next group when endtime not found
If FoundFirst030 And Cells(i, "E").Value = "" Then
FoundFirst030 = False
End If

Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top