Finding longest sequence of the same value in a row

mrshift

New Member
Joined
May 7, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Hope someone can please help with this as I've spent hours scratching my head. :)

I'm looking for a formula that calculates the green values in the J column (which I've added in manually). I need the formula to please find the longest streak in the row of consecutive instances of the same positive integer value, i.e. consecutive blank cells are not counted.

ABCDEFGHIJ
124443
2125555554
32326662323

The integer itself with the longest streak in each column isn't known, nor am I interested in knowing this - just the length of the longest unbroken sequence of that integer, please.

Hope that makes sense, and thanks in advance! (y)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Lightly tested UDF you can try. Use the UDF like you would any native worksheet function as in the example below.
Book2
ABCDEFGHIJ
124443
2125555554
32326662323
Sheet3
Cell Formulas
RangeFormula
J1:J3J1= MaxLength(A1:I1)

VBA Code:
Function MaxLength(R As Range) As Variant
Dim V As Variant, i As Long, ct As Long
V = R.Value
For i = 1 To UBound(V, 2) - 1
    If V(1, i) > 0 And V(1, i) = V(1, i + 1) Then
        If ct = 0 Then ct = 1
        ct = ct + 1
        If ct > MaxLength Then MaxLength = ct
    Else
        ct = 0
    End If
Next i
End Function
 
Upvote 0
Many thanks JoeMo :)

Unfortunately, to me it looks like it's still counting blank cells as cells that I want counted if there's a row of them. I'd gladly share my Excel file or upload a mini sheet, but it's 1,000 columns wide - any suggestions welcome :)

To explain, there are a couple of unexpected results generated from this code that currently look like this:

ABCDEFGHIJ
11234567890
29

In other words, I've have expected the result in
  1. J1 to be 1 (as there are integers in there, but all unique, so no sequences longer than 1), and
  2. J2 to be 0 (as there are no integers in there at all)
Sorry this is fiddly!
 
Upvote 0
If your row 2 is all blank cells, the function I posted returns 0 (in your J2) for me. But no matter, here's a modified function that needs more test time than I can devote to it. It would be useful if you could post some sample data, using XL2BB, that comprehends the spectrum of data contained in your excel file.
Book2
ABCDEFGHIJ
124443
2125555554
35791
41234567891
51295411
60
Sheet3
Cell Formulas
RangeFormula
J1:J6J1= MaxLength(A1:I1)

VBA Code:
Function MaxLength(R As Range) As Variant
Dim V As Variant, i As Long, ct As Long
V = R.Value
For i = 1 To UBound(V, 2) - 1
    If V(1, i) > 0 And V(1, i) = V(1, i + 1) Then
        If ct = 0 Then ct = 1
        ct = ct + 1
        If ct > MaxLength Then MaxLength = ct
    Else
        ct = 0
    End If
Next i
If MaxLength = 0 And Application.Sum(R) > 0 Then MaxLength = 1
End Function
 
Upvote 0
Many thanks again :)

Here's a small sample of the data:

Cell Formulas
RangeFormula
C1:Y1C1=B1+1
Z1:Z25Z1= MaxLength(A1:Y1)
B2:Y25B2=IFERROR(IF(B1=1,"",IF(MOD(B1,2)=0,B1/2,$A$3*B1+$A$4)),"")


It looks like the function works until it begins displaying a result of 4 or above, where it's including blank cells as cells to be counted? Using this set of data, the function shouldn't be showing any results above 2.

Thanks for your time on this!
 
Upvote 0
Using standard worksheet functions, you could try this

22 05 08.xlsm
ABCDEFGHIJ
124443
2125555554
32326662323
40
531
631
Streak
Cell Formulas
RangeFormula
J1:J6J1=IF(COUNT(A1:I1),MAX(FREQUENCY(IF(B1:I1<>"",IF(B1:I1=A1:H1,COLUMN(A1:H1))),IF(B1:I1<>"",IF(B1:I1<>A1:H1,COLUMN(A1:H1)))))+1,0)
 
Upvote 0
Sorry, I had not seen your post #5 sample data. Formula adapted to that set of columns.

Cell Formulas
RangeFormula
C1:Y1C1=B1+1
Z1:Z25Z1=IF(COUNT(A1:Y1),MAX(FREQUENCY(IF(B1:Y1<>"",IF(B1:Y1=A1:X1,COLUMN(A1:X1))),IF(B1:Y1<>"",IF(B1:Y1<>A1:A1,COLUMN(A1:X1)))))+1,0)
B2:Y25B2=IFERROR(IF(B1=1,"",IF(MOD(B1,2)=0,B1/2,$A$3*B1+$A$4)),"")
 
Upvote 0
Many thanks indeed Peter, really appreciate it :)

Though it looks like there's a snag with it - from a small excerpt of the data:

Cell Formulas
RangeFormula
ALE1:ALL1ALE1=ALD1+1
ALM1:ALM8ALM1=IF(COUNT(A1:ALL1),MAX(FREQUENCY(IF(B1:ALL1<>"",IF(B1:ALL1=A1:ALK1,COLUMN(A1:ALK1))),IF(B1:ALL1<>"",IF(B1:ALL1<>A1:A1,COLUMN(A1:ALK1)))))+1,0)
ALE2:ALL8ALE2=IFERROR(IF(ALE1=1,"",IF(MOD(ALE1,2)=0,ALE1/2,$A$3*ALE1+$A$4)),"")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A:ALJ,ALM:XEZCellcontains a blank value textNO
A:ALJ,ALM:XEZExpression=OR(OFFSET(B1,0,-1)=OFFSET(A1,0,-1),OFFSET(B1,0,-1)=OFFSET(C1,0,-1))textNO
ALL250:ALL1048576,ALL1:ALL19Cellcontains a blank value textNO
ALL250:ALL1048576,ALL1:ALL19Expression=OR(OFFSET(#REF!,0,-1)=OFFSET(ALL1,0,-1),OFFSET(#REF!,0,-1)=OFFSET(#REF!,0,-1))textNO
ALK248:ALK1048576,ALK1:ALK19Cellcontains a blank value textNO
ALK248:ALK1048576,ALK1:ALK19Expression=OR(OFFSET(ALL1,0,-1)=OFFSET(ALK1,0,-1),OFFSET(ALL1,0,-1)=OFFSET(#REF!,0,-1))textNO


The bottom two rows should be reporting a 3, not a 2?
 
Upvote 0
PS sorry for the crazy conditional formatting - accidentally included it! ?‍♂️
 
Upvote 0
Though it looks like there's a snag with it
Yes, in post #7 I did not correctly adjust the post #6 formula. :oops:

The formula in ALM1 of post #8 is
=IF(COUNT(A1:ALL1),MAX(FREQUENCY(IF(B1:ALL1<>"",IF(B1:ALL1=A1:ALK1,COLUMN(A1:ALK1))),IF(B1:ALL1<>"",IF(B1:ALL1<>A1:A1,COLUMN(A1:ALK1)))))+1,0)

It should be
=IF(COUNT(A1:ALL1),MAX(FREQUENCY(IF(B1:ALL1<>"",IF(B1:ALL1=A1:ALK1,COLUMN(A1:ALK1))),IF(B1:ALL1<>"",IF(B1:ALL1<>A1:ALK1,COLUMN(A1:ALK1)))))+1,0)
 
Upvote 0

Forum statistics

Threads
1,224,005
Messages
6,175,910
Members
452,682
Latest member
ghorne

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