Lookup last pattern (ant list the row nº+interval)

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Hi,

My patterns in range N5:W5 I want lookup each pattern in column C:G and if find last in the column list row+interval in the range N3:W4

For example pattern 1|1|1|1|1 find in columns C+D+E+F+G in the rows 7 , 9, 14, 19, 21 & last in the row 24 so I want note the Row nº in cell N3 & N4 = 3 the interval show in cell H24 (in the same pattern row column H)
The same method applied to rest patterns

Example results


Book1
ABCDEFGHIJKLMNOPQRSTUVW
1
2
3Row n244716324129153059
4Interval3394015120713
5P1P2P3P4P5Interval1|1|1|1|11|1|1|1|X1|1|1|1|21|1|1|X|11|1|1|X|X1|1|1|X|21|1|1|2|11|1|1|2|X1|1|1|2|21|1|X|1|1
6P1P2P3P4P5Interval
7111110
81111X0
9111112
10111220
111X1X10
12111120
13X11210
14111115
151112X0
16111124
17111210
18111228
19111115
20X11XX0
21111112
22111224
23111221
24111113
25X112112
26111XX0
27X11110
2812XXX0
291112112
30111227
31X2XX20
32111X10
331X1120
342211X0
351X1210
3611X110
371XX210
38X111111
39X211X0
40X12120
41111XX15
42XXX110
432X21X0
44XX12X0
45XX1X10
4611X1110
471111X39
482X1110
49121X20
50X1X120
51X111113
52X1X110
53X111X0
54X11220
5521X210
56XX21X0
571X2X10
58X2X110
5911X1113
Row+Interval


Thank you in advance

Using Excel 2000
Regards,
Kishan

 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
Public Sub LookupLastPattern()

Dim rowIntervals As New Collection
Dim patternCell As Range
Dim lastRow As Long
Dim pattern As String
Dim currentValue As String
Dim thisRow As Long
Dim thisCol As Long

On Error Resume Next

lastRow = Cells(Rows.Count, 3).End(xlUp).Row
For thisRow = 7 To lastRow
    pattern = ""
    For thisCol = 3 To 7
        pattern = pattern & "|" & CStr(Cells(thisRow, thisCol))
    Next
    pattern = Mid$(pattern, 2)
    Err.Clear
    currentValue = rowIntervals.Item(pattern)
    If Err.Number <> 0 Then
        rowIntervals.Add "@", pattern
        currentValue = "@"
    End If
    
    If currentValue = "@" Then
        Cells(thisRow, 8).Value = 0
        rowIntervals.Remove pattern
        rowIntervals.Add CStr(thisRow) & ",0", pattern
    Else
        Cells(thisRow, 8).Value = thisRow - CLng(Split(currentValue, ",")(0))
        rowIntervals.Remove pattern
        rowIntervals.Add CStr(thisRow) & "," & CStr(Cells(thisRow, 8).Value), pattern
    End If
Next

For thisCol = 14 To 23
    Err.Clear
    currentValue = rowIntervals.Item(Cells(5, thisCol).Value)
    If Err.Number <> 0 Then
        Cells(3, thisCol).ClearContents
        Cells(4, thisCol).ClearContents
    Else
        Cells(3, thisCol) = Split(currentValue, ",")(0)
        Cells(4, thisCol) = Split(currentValue, ",")(1)
    End If
Next

End Sub

WBD
 
Upvote 0
Assuming you have less than 65,536 rows of data, here is another macro for you to consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub LastPattern()
  Dim C As Long, LastRow As Long, Position As Long
  Dim Found As String, Combined As String
  Dim Data As Variant, Patterns As Variant, Result As Variant
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  ReDim Result(1 To 2, 1 To 10)
  Range("N3:W4").Clear
  Combined = Join(Application.Transpose(Evaluate(Replace("C7:C#&""|""&D7:D#&""|""&E7:E#&""|""&F7:F#&""|""&G7:G#&""@""&H7:H#&""@""&ROW(7:#)", "#", LastRow))), "@")
  Patterns = Range("N5:W5")
  For C = 1 To UBound(Patterns, 2)
    Position = InStrRev(Combined, Patterns(1, C))
    If Position Then
    Data = Split(Mid(Combined, Position), "@")
      Result(1, C) = Data(2)
      Result(2, C) = Data(1)
    End If
  Next
  Range("N3").Resize(2, 10) = Result
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Code:
Public Sub LookupLastPattern()

Dim rowIntervals As New Collection
Dim patternCell As Range
Dim lastRow As Long
Dim pattern As String
Dim currentValue As String
Dim thisRow As Long
Dim thisCol As Long

On Error Resume Next

lastRow = Cells(Rows.Count, 3).End(xlUp).Row
For thisRow = 7 To lastRow
    pattern = ""
    For thisCol = 3 To 7
        pattern = pattern & "|" & CStr(Cells(thisRow, thisCol))
    Next
    pattern = Mid$(pattern, 2)
    Err.Clear
    currentValue = rowIntervals.Item(pattern)
    If Err.Number <> 0 Then
        rowIntervals.Add "@", pattern
        currentValue = "@"
    End If
    
    If currentValue = "@" Then
        Cells(thisRow, 8).Value = 0
        rowIntervals.Remove pattern
        rowIntervals.Add CStr(thisRow) & ",0", pattern
    Else
        Cells(thisRow, 8).Value = thisRow - CLng(Split(currentValue, ",")(0))
        rowIntervals.Remove pattern
        rowIntervals.Add CStr(thisRow) & "," & CStr(Cells(thisRow, 8).Value), pattern
    End If
Next

For thisCol = 14 To 23
    Err.Clear
    currentValue = rowIntervals.Item(Cells(5, thisCol).Value)
    If Err.Number <> 0 Then
        Cells(3, thisCol).ClearContents
        Cells(4, thisCol).ClearContents
    Else
        Cells(3, thisCol) = Split(currentValue, ",")(0)
        Cells(4, thisCol) = Split(currentValue, ",")(1)
    End If
Next

End Sub

WBD
Brilliant!! WBD, it is resulting as treat.

I appreciate your kind support

Regards,
Kishan
 
Upvote 0
Assuming you have less than 65,536 rows of data, here is another macro for you to consider...
Hi,

Rick Rothstein Thanks. I appreciate your help.

Code works perfect with sample data when I tried with original data it stop at the line below and highlights the line in colour yellow


Code:
 Combined = Join(Application.Transpose(Evaluate(Replace("C7:C#&""|""&D7:D#&""|""&E7:E#&""|""&F7:F#&""|""&G7:G#&""@""&H7:H#&""@""&ROW(7:#)", "#", LastRow))), "@")

Please coluld you try the code after multiplying rows and see does the macro reproduce error with you. May be it is small adjustment to do.

Sample data there are 53 rows (53*1000 = 53000 + 6 = 53006) copy 53 rows, select range C60:G53006 and paste them then run the macro.

Thank you

Regards,
Kishan
 
Upvote 0
Sample data there are 53 rows (53*1000 = 53000 + 6 = 53006) copy 53 rows, select range C60:G53006 and paste them then run the macro.
I took your sample data and repeatedly copied it to create a table which started at C7 and ended at G53006 and the code I posted worked fine for that enlarged data set.... so I cannot recreate your problem here. I notice you wrote the start as of your data as C60, not C7 as your sample data showed. Is C60 the real start of your data? If so, then perhaps you have values in cells C7:G59 that are affecting my code in some way. Assuming your data really starts at row 60, see if this modification to my code works for that setup (I still assumed your patterns were in cells N5:W5)...
Code:
[table="width: 500"]
[tr]
	[td]Sub LastPattern()
  Dim C As Long, LastRow As Long, Position As Long
  Dim Found As String, Combined As String
  Dim Data As Variant, Patterns As Variant, Result As Variant
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  ReDim Result(1 To 2, 1 To 10)
  Range("N3:W4").Clear
  Combined = Join(Application.Transpose(Evaluate(Replace("C60:C#&""|""&D60:D#&""|""&E60:E#&""|""&F60:F#&""|""&G60:G#&""@""&H60:H#&""@""&ROW(60:#)", "#", LastRow))), "@")
  Patterns = Range("N5:W5")
  For C = 1 To UBound(Patterns, 2)
    Position = InStrRev(Combined, Patterns(1, C))
    If Position Then
    Data = Split(Mid(Combined, Position), "@")
      Result(1, C) = Data(2)
      Result(2, C) = Data(1)
    End If
  Next
  Range("N3").Resize(2, 10) = Result
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
I took your sample data and repeatedly copied it to create a table which started at C7 and ended at G53006 and the code I posted worked fine for that enlarged data set.... so I cannot recreate your problem here. I notice you wrote the start as of your data as C60, not C7 as your sample data showed. Is C60 the real start of your data? If so, then perhaps you have values in cells C7:G59 that are affecting my code in some way. Assuming your data really starts at row 60, see if this modification to my code works for that setup (I still assumed your patterns were in cells N5:W5)...
Hi,

Rick Rothstein, my data start in C7 I said (multiplying rows) copy 53 sample data rows from C60 to down
Tried modified code using data starts as C60 but code stop at the same line

As post#3 code was working perfect with sample data to I wanted to check now till what end of the row code work, found It works with last row 5467 that give me impression (your code is nice) but as I am not update with version Excel 2000 does not support transpose row more than 5460 it permits only max rows 5460 (5467-7 as starting row)

Rick, I am sorry to making you work twice it is not your code it is version problem which I am using

Thank you once again for your kind help

Regards,
Kishan
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,834
Members
449,471
Latest member
lachbee

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