Count consecutive match with previous row

Kishan

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

Hi,

I need a formula to Count MAX Consecutive Match with previous row data
For example:
Check row8 with row7 find 2 consecutive matches
Check row12 with row11 find 4 consecutive matches
Check row16 with row15 find 3 consecutive matches
Check row20 with row19 find 8 consecutive matches

Example data...


Book1
ABCDEFGHIJKLMNOPQR
1
2
3
4
5Num1234567891011121314Count MAX Consecutive Match
6
7X11121212X11X1
8XX21XX1121X2112
9
10
11X1X212XXX111XX
1211X121XXX121X14
13
14
1521X11X11121112
161111121121XXX13
17
18
1911111211121112
201111121121XXX18
21
22
Sheet1


Thank you in advance

Kishan
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
No idea on a formula that could do this, but I could do it using VBA. Is VBA an option for you or does it have to be a formula?

A custom function doesn't seem too difficult for this. I'll edit this in a bit with a formula
 
Upvote 0
Code:
Function ConsecutiveMatches(arr1 As Range, arr2 As Range) As Long


If arr1.Count <> arr2.Count Then
    ConsecutiveMatches = -1
    Exit Function
End If


Dim maxMatches As Long: maxMatches = 0
Dim i As Long: i = arr1.Count


For n = 1 To i
    If arr1(n) = arr2(n) Then
        maxMatches = maxMatches + 1
    Else
        If maxMatches > ConsecutiveMatches Then
            ConsecutiveMatches = maxMatches
        End If
        maxMatches = 0
    End If
Next n


' for the case where max matches at end of array
If maxMatches > ConsecutiveMatches Then
    ConsecutiveMatches = maxMatches
End If


End Function
 
Upvote 0
Question
Why should the result for the set Row 15 - Row 16 be 3?
Neither M16 nor N16 nor O16 are the same as above.
Could you clarify?

M.
 
Upvote 0
Question
Why should the result for the set Row 15 - Row 16 be 3?
Neither M16 nor N16 nor O16 are the same as above.
Could you clarify?

M.
Hi Marcelo Branco, sorry my error here is the correct layout answer in the cell R16 should be 2.


Book1
ABCDEFGHIJKLMNOPQR
1
2
3
4
5Num1234567891011121314Count MAX Consecutive Match
6
7X11121212X11X1
8XX21XX1121X2112
9
10
11X1X212XXX111XX
1211X121XXX121X14
13
14
1521X11X11121112
161111121121XXX12
17
18
1911111211121112
201111121121XXX18
21
22
Sheet1


Thank you

Regards,
Kishan
 
Upvote 0
No idea on a formula that could do this, but I could do it using VBA. Is VBA an option for you or does it have to be a formula?

A custom function doesn't seem too difficult for this. I'll edit this in a bit with a formula

Code:
Function ConsecutiveMatches(arr1 As Range, arr2 As Range) As Long


If arr1.Count <> arr2.Count Then
    ConsecutiveMatches = -1
    Exit Function
End If


Dim maxMatches As Long: maxMatches = 0
Dim i As Long: i = arr1.Count


For n = 1 To i
    If arr1(n) = arr2(n) Then
        maxMatches = maxMatches + 1
    Else
        If maxMatches > ConsecutiveMatches Then
            ConsecutiveMatches = maxMatches
        End If
        maxMatches = 0
    End If
Next n


' for the case where max matches at end of array
If maxMatches > ConsecutiveMatches Then
    ConsecutiveMatches = maxMatches
End If


End Function
Hi Ells_, I tried the "Function ConsecutiveMatches" it is prefect even for cell R16 it is giving the answer 2 which is correct "I did not noticed till Marcelo Branco mentioned that I had error in the row15 & 16"

It will be ok if you could make a VBA too.

Thank you

Regards,
Kishan
 
Upvote 0
A formula solution

Array formula in R8
=MAX(FREQUENCY(IF(C7:P7=C8:P8,COLUMN(C7:P7)),IF(C7:P7<>C8:P8,COLUMN(C7:P7))))
Ctrl+Shift+Enter

Then copy this formula (Ctrl+C)
and paste (Ctrl+V) in R12, R16, R20

Hope this helps

M.
 
Upvote 0
A formula solution

Array formula in R8
=MAX(FREQUENCY(IF(C7:P7=C8:P8,COLUMN(C7:P7)),IF(C7:P7<>C8:P8,COLUMN(C7:P7))))
Ctrl+Shift+Enter

Then copy this formula (Ctrl+C)
and paste (Ctrl+V) in R12, R16, R20

Hope this helps

M.
Hi Marcelo Branco, formula is resulting as request.

Thank you for your help

Regards,
Kishan
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,569
Members
452,652
Latest member
eduedu

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