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.
 

Some videos you may like

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
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:

 
Solution

paulrk

New Member
Joined
Nov 10, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
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
Joined
Apr 25, 2006
Messages
19,884
Remark: A simpler expression for the return value

replace:

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

with

Code:
LongestStreak = dic.Count
 

Watch MrExcel Video

Forum statistics

Threads
1,128,018
Messages
5,628,170
Members
416,297
Latest member
Kara Payne

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
Top