Counting longest non-repeating streak

paulrk

New Member
Hi, I’d appreciate help to calculate a tourism streak or longest streak of different events done before one is repeated. I'm using Office 365 on a Windows PC.

In the example below I have entered events done and the order they were done in plus a third column with my manual entry of the count from the start of that streak until it is broken that I would like to replace with a formula. Once this calculation is automated I will be able to look up 6 as the max value.

Please note that the longest overall streak is 9 between repeats of Event A, but in the interim there were two visits to Event E that cuts short the streak to 6.

Any help greatly appreciated, although I’ve not used VBA before so I’m hoping for a formula-based solution if at all possible.

Thanks,

Paul.

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

pgc01

MrExcel MVP
Hi Paul
Welcome to the board

Try this udf.

VBA Code:
Function LongestStreak(r As Range)
Static dic As Object
Dim j As Long

If dic Is Nothing Then Set dic = CreateObject("Scripting.Dictionary")
dic.RemoveAll

For j = r.Rows.Count To 1 Step -1
If Not dic.exists(r(j).Value) Then dic.Add r(j).Value, "" Else Exit For
Next j
LongestStreak = r.Rows.Count - j
End Function

Example. In B2:

=LongestStreak(\$A\$2:A2)
Copy down

Book1
ABC
1
2a1
3a1
4b2
5b1
6c2
7d3
8e4
9c3
10f4
11g5
12h6
13i7
14j8
15g4
16i3
17
Sheet1

Remark: If you are not used to udf's, check Rick's istructions in this post:

paulrk

New Member
Thanks, PGC! It works perfectly. I spent days staring at this problem and it was driving me to distraction. You've made my day and I'm very grateful

pgc01

MrExcel MVP
You're welcome.
Thanks for the feedback.

pgc01

MrExcel MVP
Remark: A simpler expression for the return value

replace:

VBA Code:
LongestStreak = r.Rows.Count - j

with

Code:
LongestStreak = dic.Count

Replies
1
Views
107
Replies
3
Views
333
Replies
1
Views
271
Replies
0
Views
1K
Replies
1
Views
595

1,126,939
Messages
5,621,722
Members
415,853
Latest member
Newlife72

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.

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

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