Find patterns in data points

Ghris

Board Regular
Joined
Mar 17, 2012
Messages
73
Hello,

I wonder if there is a way to check for a certain pattern in a list of data points.

This image might clarify what I am looking for. I need a way to check if the pattern exists (and where) in the data points.
excelproblem.jpg


I have tried with vlookup but only got it to work with finding 1 number.
 
Ghris,

I am having a problem downloading your huge workbook.

Can you post another workbook, with just 1000 rows of raw data?

Let me see what I can do with the screenshot from Snakehips.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Ghris,

I am having a problem downloading your huge workbook.

Can you post another workbook, with just 1000 rows of raw data?

Let me see what I can do with the screenshot from Snakehips.
Oh good... I thought it was just my computer (which I have rebooted twice now hoping to solve the download problem).
Also working with the layout that Snakehips shows, here is my code revised to highlight the cells in Column A (that match the pattern) in yellow and to list the starting row numbers in Column H for each location..
Code:
Sub FindPattern()
  Dim X As Long, Index As Long, Commas As Long, Rw As Long, PatLen As Long, RowNums As Variant
  Dim DataPoints As String, Pattern As String, Parts() As String
  ReDim RowNums(1 To Cells(Rows.Count, "A").End(xlUp).Row, 1 To 1)
  DataPoints = "," & Join(Application.Transpose(Range("A2", Cells(Rows.Count, "A").End(xlUp))), ",,") & ","
  Pattern = "," & Join(Application.Transpose(Range("D3", Cells(Rows.Count, "D").End(xlUp))), ",,") & ","
  Columns("A").Interior.ColorIndex = xlColorIndexNone
  Parts = Split(DataPoints, Pattern)
  PatLen = UBound(Split(Replace(Pattern, ",,", ","), ",")) - 1
  Rw = 2
  For X = 0 To UBound(Parts) - 1
    Commas = UBound(Split(Replace(Parts(X), ",,", ","), ","))
    Rw = Rw + Commas + 1 - 2 * (Len(Parts(X)) = 0)
    Cells(Rw - 2, "A").Resize(PatLen).Interior.ColorIndex = 6
    Index = Index + 1
    RowNums(Index, 1) = Rw - 2
    Rw = Rw + PatLen - 2
  Next
  Range("H4:H" & UBound(RowNums)) = RowNums
End Sub
 
Upvote 0
Ghris,

My latest macro will fail/not produce the correct results, if cell A1 = D3. Let me see if I can work out the correct code, for the above case.


After my latest macro:


Excel 2007
ABCDEFGHIJKL
1136.19
21.72Look for the following pattern:Result:Matches
31.061.77The pattern occurs at this row(s):1
41.093.1916
56.552.99
62.131.15
72.192.59
81173.541.09
92.141.75
102.234.53
1111.511.03
121.473.74
131.88
149.15
1586.81
161.77
173.19
182.99
191.15
202.59
211.09
221.75
234.53
241.03
253.74
265.18
275.41
28
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

If you place the below macro code in a module that already has Option Base 1, you will have to delete one of these lines of code.


Code:
Option Base 1
Sub FindPatterns_V2()
' hiker95, 11/23/2014, ME820043
Dim p As Variant, o As Variant
Dim i As Long, j As Long
Dim lra As Long, lrp As Long
Dim a As Range
Dim n As Long, nc As Long, sr As Long, nr As Long, pc As Long, nf As Long
n = Application.CountIf(Columns(1), Cells(3, 4).Value)
If n = 0 Then
  MsgBox ("There are no '" & Cells(3, 4).Value & "' values in column A - macro terminated!")
  Exit Sub
End If
Application.ScreenUpdating = False
ReDim o(1 To n, 1 To 1)
lra = Cells(Rows.Count, 1).End(xlUp).Row
lrp = Cells(Rows.Count, 4).End(xlUp).Row
p = Range("D3:D" & lrp)
sr = 1
For nc = 1 To n
  Set a = Range("A" & sr & ":A" & lra).Find(Range("D3").Value, LookAt:=xlWhole)
  If Not a Is Nothing Then
    pc = 1: nr = a.Row
    For i = 2 To UBound(p, 1)
      nr = nr + 1
      If nr > lra Then GoTo MyFinish
      If Cells(nr, 1) = p(i, 1) Then
        pc = pc + 1
      End If
    Next i
    If pc = UBound(p, 1) Then
      j = j + 1
      o(j, 1) = a.Row
      nf = nf + 1
    End If
  End If
  sr = a.Row
Next nc
MyFinish:
Cells(4, 8).Resize(, UBound(o, 2)).Value = o
Cells(3, 12) = nf
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the FindPatterns_V2 macro.
 
Upvote 0
Also working with the layout that Snakehips shows, here is my code revised to highlight the cells in Column A (that match the pattern) in yellow and to list the starting row numbers in Column H for each location..
Code:
Sub FindPattern()
....
....
End Sub
Whoops! I missed that Snakehips' layout started the values in Column A at Row 1 (no header) rather than at Row 2 (with A1 being a header) like I originally assumed. This code uses Snakehips' layout exactly (data points start in cell A1, pattern starts in cell D3 and outputted Row numbers start in cell H4)...
Code:
Sub FindPattern()
  Dim X As Long, Index As Long, Commas As Long, Rw As Long, PatLen As Long, RowNums As Variant
  Dim DataPoints As String, Pattern As String, Parts() As String
  ReDim RowNums(1 To Cells(Rows.Count, "A").End(xlUp).Row, 1 To 1)
  DataPoints = "," & Join(Application.Transpose(Range("A1", Cells(Rows.Count, "A").End(xlUp))), ",,") & ","
  Pattern = "," & Join(Application.Transpose(Range("D3", Cells(Rows.Count, "D").End(xlUp))), ",,") & ","
  Columns("A").Interior.ColorIndex = xlColorIndexNone
  Parts = Split(DataPoints, Pattern)
  PatLen = UBound(Split(Replace(Pattern, ",,", ","), ",")) - 1
  Rw = 2
  For X = 0 To UBound(Parts) - 1
    Commas = UBound(Split(Replace(Parts(X), ",,", ","), ","))
    Rw = Rw + Commas + 1 - 2 * (Len(Parts(X)) = 0)
    Cells(Rw - 3, "A").Resize(PatLen).Interior.ColorIndex = 6
    Index = Index + 1
    RowNums(Index, 1) = Rw - 3
    Rw = Rw + PatLen - 2
  Next
  Range("H4:H" & UBound(RowNums)) = RowNums
End Sub
 
Upvote 0
Sorry for the delay, was all but ready to post this over two hours ago but life interrupted ;)

Given the posted layout, this should deal with as much data as you like in A and with a variable number of values for the Pattern in D, and is none too sluggish.



Excel 2007
ABCDEFGHIJ
1136.19
21.72Look for the following pattern:Result:
31.061.77The pattern occurs at this cell(s):
41.093.19A1612Matches
56.552.99A700
62.131.15A60000
72.192.59A98696
81173.541.09A98853
92.141.75A197386
102.234.53A408297
1111.511.03A408628
121.473.74A1014206
131.88A1014333
149.15A1014419
1586.81A1014630
161.77
173.19
Sheet2


Code:
Sub Pattern_Match()


Dim Drng As Range
Dim Prng As Range
Dim Found As Range
Dim LstA, LstD, LstH, MtchCount, c  As Long
Dim Mtch As Integer
Dim PArry As Variant


Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual


LstA = Cells(Rows.Count, "A").End(xlUp).Row
LstD = Cells(Rows.Count, "D").End(xlUp).Row
LstH = Cells(Rows.Count, "H").End(xlUp).Row
If LstH < 4 Then LstH = 4
Set Drng = Range("A1:A" & LstA)
Set Prng = Range("D3:D" & LstD)
Set Hrng = Range("H4:D" & LstH)
Range("H4:H" & LstH).ClearContents
Range("I4").ClearContents
If LstA = 1 Or LstD = 2 Then Exit Sub
PArry = Prng
MtchCount = 0
Set LstCell = Drng.Cells(Drng.Cells.Count, 1)
Set Found = Drng.Find(What:=PArry(1, 1), After:=LstCell)
If Not Found Is Nothing Then
    First = Found.Address
End If
Do Until Found Is Nothing
    Mtch = 1
        For c = LstD - 2 To 2 Step -1
            If Not PArry(c, 1) = Found.Offset(c - 1, 0) Then
                Mtch = 0
                Exit For
            End If
        Next c
        
    If Mtch = 1 Then
        MtchCount = MtchCount + 1
        Range("H3").Offset(MtchCount, 0) = Found.Address(False, False)
    End If
    
    Set Found = Drng.FindNext(After:=Found)
    If Found.Address = First Then Exit Do
     
Loop
Range("I4") = MtchCount
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic


End Sub

Hope that helps.
 
Upvote 0
Given the posted layout, this should deal with as much data as you like in A and with a variable number of values for the Pattern in D, and is none too sluggish.
Were you able to down the OP's file from his link? For some reason I cannot (and it looks like hiker95 cannot either). If so, can I ask you to do me a favor and run the code I posted in Message #15 and tell me (us) how it does? Thanks.
 
Upvote 0
Were you able to down the OP's file from his link? For some reason I cannot (and it looks like hiker95 cannot either). If so, can I ask you to do me a favor and run the code I posted in Message #15 and tell me (us) how it does? Thanks.

Hi Rick, Hiker,

Yes I was able to download the OP's file and the inconsistent header rows are as per that download.

A1:A277775 contained values > 0 but then the remaining 700,000+ rows contained nothing but zeros!!! So it was clogged up with nothing, big time!!
There was no obvious match for the OP pattern and no means to find any until a solution was developed so I copied a random 10 cells from A16 and pasted them here and there for testing.
I have subsequently found that there was just one OP pattern match at row 235340!!!!

Rick, Initially, I tried your code on my original test file and it ran but returned nothing at all. ????
However, given Hiker's request to OP to post a smaller file, I trimmed it down to 1000 rows . On that reduced data set, your code worked perfectly!, returning the same set of results as my code.

Is it some Excel restriction on array size that would have caused that?

If you wish me to upload the reduced file for you access then just let me know. (But don't delay too much as some of us mere mortals need to go to bed!! ;))
 
Last edited:
Upvote 0
Rick, Initially, I tried your code on my original test file and it ran but returned nothing at all. ????
However, given Hiker's request to OP to post a smaller file, I trimmed it down to 1000 rows . On that reduced data set, your code worked perfectly!, returning the same set of results as my code.

Is it some Excel restriction on array size that would have caused that?

If you wish me to upload the reduced file for you access then just let me know. (But don't delay too much as some of us mere mortals need to go to bed!! ;))

If you wish me to upload the reduced file for you access then just let me know. (But don't delay too much as some of us mere mortals need to go to bed!! ;))
Could I ask you to email it to me? Also the original file that it did not work on? My email addess is...

rick DOT news AT verizon DOT net
 
Upvote 0
Ghris,

If we start off with this, we get this:


Excel 2007
ABCDEFGHIJKL
11.77
23.19Look for the following pattern:Result:Matches
32.991.77The pattern occurs at this row(s):0
41.153.19
52.592.99
61.15
72.59
81.09
91.75
104.53
111.03
123.74
13
Sheet1


If we start off with this, we get this:


Excel 2007
ABCDEFGHIJKL
11.77
23.19Look for the following pattern:Result:Matches
32.991.77The pattern occurs at this row(s):1
41.153.196
52.592.99
61.771.15
73.192.59
82.991.09
91.151.75
102.594.53
111.091.03
121.753.74
134.53
141.03
153.74
16
Sheet1


If we start off with this, we get this:


Excel 2007
ABCDEFGHIJKL
11.77
23.19Look for the following pattern:Result:Matches
32.991.77The pattern occurs at this row(s):1
41.153.1920
5136.192.99
61.721.15
71.062.59
81.091.09
96.551.75
102.134.53
112.191.03
121173.543.74
132.14
142.23
1511.51
161.47
171.88
189.15
1986.81
201.77
213.19
222.99
231.15
242.59
251.09
261.75
274.53
281.03
293.74
305.18
315.41
32
Sheet1


See my next reply for the macro code.
 
Upvote 0

Forum statistics

Threads
1,215,963
Messages
6,127,954
Members
449,412
Latest member
montand

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