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)
 
I assume you will want to use Peter's worksheet functions solution, but for the record, the reason the UDF I proposed fails is because your "empty" (blank) cells contain formulas that return the null string ('"") which you did not mention in your OP.

Below is a modified UDF that I've only tested successfully on the data you furnished in Post #5. Should you have time to test it on your more extensive data, I'd be curious to know if it works for you.
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) = "" Then ct = 0
    If V(1, i) <> "" And V(1, i + 1) <> "" Then
        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
    End If
Next i
If MaxLength = 0 And Application.Sum(R) > 0 Then MaxLength = 1
End Function
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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