ID Batches of Time Events within x seconds of each other

JonRowland

Active Member
Joined
May 9, 2003
Messages
417
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to work out the best way to solve a problem. I have a worksheet containing thousands of timed events, sorted in date/time order. Within this date there will be some batch events which occured with a short period. What I would like is to somehow hightlight or similar periods where there are a series of 5 or more events all within say a period 10 seconds or less of each other.

For examle the data (cols A=Date , B = Time) below I would want those in red highlight (probably with a reference in the LastCol +1)

01/01/2022 10:44:01
01/01/2022 10:44:03
01/01/2022 10:44:05
01/01/2022 10:44:07
01/01/2022 10:44:10

01/01/2022 10:45:00
01/01/2022 10:45:02
01/01/2022 10:54:11
01/01/2022 10:54:18
01/01/2022 10:54:23
01/01/2022 10:54:25
01/01/2022 10:54:29

01/01/2022 11:00:00

I'm thinking VBA for quickness but struggling to think how I'd do this. I think Do...While whislt the difference is <10 seconds between the times - but not sure. Any suggestions to get me start would be great.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
@JonRowland
Try this:
VBA Code:
Sub JonRowland_2()
Dim i As Long, j As Long, k As Long, n As Long, ub As Long
Dim va, vb

Application.ScreenUpdating = False

rc = ActiveSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1 'last column + 1

vb = Range("A1:B" & Cells(Rows.Count, "A").End(xlUp).Row + 1)
ub = UBound(vb, 1)
ReDim va(1 To ub, 1 To 1)

For i = 1 To ub
    va(i, 1) = vb(i, 1) + vb(i, 2)
Next

va(ub, 1) = CDate("1-1-2100")

For i = 2 To UBound(va, 1) - 5   'data start at row 2
    j = i:    k = i:    n = 1
    
    Do
        
        If DateDiff("s", va(j, 1), va(j + 1, 1)) < 10 Then
            j = j + 1:   n = n + 1
        Else
            If n > 4 Then
                Range(Cells(k, "A"), Cells(j, "B")).Font.Color = vbRed
                Range(Cells(k, rc), Cells(j, rc)).Value = "x"            'put "x" in last col+1
            End If
            Exit Do
        End If
        
     
     Loop While j < UBound(va, 1)
    
Next

Application.ScreenUpdating = True

End Sub

Book1
ABCDE
1date timesomething
201/01/202210.44.01x
301/01/202210.44.03x
401/01/202210.44.05x
501/01/202210.44.07x
601/01/202210.44.10x
701/01/202210.45.00
801/01/202210.45.02
901/01/202210.54.11x
1001/01/202210.54.18x
1101/01/202210.54.23x
1201/01/202210.54.25x
1301/01/202210.54.29x
1401/01/202211.00.00
Sheet3
 
Upvote 0
Solution
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0
@Akuini - one little extra would be to add a counter. I've tried to work out where it would be best to place it but have failed. Can you help?

1658240576233.png
 
Upvote 0
one little extra would be to add a counter.
Try:
VBA Code:
Sub JonRowland_2()
Dim i As Long, j As Long, k As Long, n As Long, ub As Long, h As Long
Dim va, vb

Application.ScreenUpdating = False

rc = ActiveSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1 'last column + 1

vb = Range("A1:B" & Cells(Rows.Count, "A").End(xlUp).Row + 1)
ub = UBound(vb, 1)
ReDim va(1 To ub, 1 To 1)

For i = 1 To ub
    va(i, 1) = vb(i, 1) + vb(i, 2)
Next

va(ub, 1) = CDate("1-1-2100")

For i = 2 To UBound(va, 1) - 5   'data start at row 2
    j = i:    k = i:    n = 1
    
    Do
        
        If DateDiff("s", va(j, 1), va(j + 1, 1)) < 10 Then
            j = j + 1:   n = n + 1
        Else
            If n > 4 Then
                Range(Cells(k, "A"), Cells(j, "B")).Font.Color = vbRed
                h = h + 1
                Range(Cells(k, rc), Cells(j, rc)).Value = "x-" & Format(h, "000")           'put "x" in last col+1
            End If
            Exit Do
        End If
        
     
     Loop While j < UBound(va, 1)
    
Next

Application.ScreenUpdating = True

End Sub
 
Upvote 0

@Akuini

Thanks but doesn't give the result I was looking for. Works for the first loop for range is tagged x-001 but as your loop restarts and then steps through row+1 etc means it ends up with X-001,X-002,X-003 where I was hoping for just X-001 then a new series of X-002

So I made a little change and seems to work which appears to work

<code>
For i = 2 To UBound(va, 1) - 5 'data start at row 2
j = i: k = i: n = 1

Do

If DateDiff("s", va(j, 1), va(j + 1, 1)) < 10 Then
j = j + 1: n = n + 1
Else
If n > 4 Then
Range(Cells(k, "A"), Cells(j, "B")).Font.Color = vbRed
h = h + 1
Range(Cells(k, rc), Cells(j, rc)).Value = "x-" & Format(h, "000") 'put "x" in last col+1
i = i + j ' Increment i by the last in the series range "J"
End If
Exit Do
End If
</code>

So in the image C is your original code and D is my slight amendment but guessing I am needing some confirmation that won't miss things as the original code you kindly provided seems to step through.

1658254733239.png
 
Upvote 0
So in the image C is your original code and D is my slight amendment
Are you sure?
Here's the result using my code in post #6.
Isn't it what you want?
Book1
ABCDE
1date timesomething
2445620.447234x-001
3445620.447257x-001
4445620.44728x-001
5445620.447303x-001
6445620.447338x-001
7445620.447917
8445620.44794
9445620.454294x-002
10445620.454375x-002
11445620.454433x-002
12445620.454456x-002
13445620.454502x-002
14445620.458333
Sheet1
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
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