Counting longest non-repeating streak

paulrk

New Member
Joined
Nov 10, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
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.

Non-repeating streak example.PNG


Thanks,

Paul.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
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:

 
Upvote 0
Solution
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 :)
 
Upvote 0
Remark: A simpler expression for the return value

replace:

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

with

Code:
LongestStreak = dic.Count
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,952
Members
448,535
Latest member
alrossman

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