Count a values' consecutive duplicates in a range

loudnoiseman

Board Regular
Joined
Dec 31, 2004
Messages
218
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'd like to count how many times a numerical value repeats in a range as a consecutive duplicate - I didn't see any related posts...

For example, the value is in A2 and the range is D2:Z2 and I'd like to place the formula in B2 to return the # of consecutive duplicates.

With comma's separating cells, let's say value is 3 and range is: 4,6,8,3,3,5,3,3,8,9,2 etc.

In this short excerpt the result would be 2.

Thanks!!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this array formula** :

=SUM(IF(FREQUENCY(IF(D2:Z2=A2,COLUMN(D2:Z2)),IF(D2:Z2<>A2,COLUMN(D2:Z2)))>1,1))

** array formulas need to be entered using the key combination of CTRL,SHIFT,ENTER (not just ENTER). Hold down both the CTRL key and the SHIFT key then hit ENTER.
 
Upvote 0
try

=SUMPRODUCT(--(D2:Z2=A2),--(E2:AA2=A2),--(F2:AB2<>A2))
 
Upvote 0
Good question Seiya,

the result in your example should be 5 whereas formula suggestions from sanrv1f & #Name? result in 2...

I guess I was hoping for a process that would count all instances where the value is found and the next cell is equal...

hmmm

well, thanks for suggestions
 
Upvote 0
then, remove the last part of my formula

=SUMPRODUCT(--(D2:Z2=A2),--(E2:AA2=A2))
 
Upvote 0
UDF

=CountConsecutive(D2:Z2,3)

Code:
Function CountConsecutive(ByRef rng As Range, myNum) As Long
Dim a, i As Long
a = rng.Value
For i = 1 To UBound(a, 2) - 1
    If(a(1, i + 1) = myNum) * (a(1, i) = myNum) Then
        CountConsecutive = CountConsecutive + 1
    End If
Next
End Function
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
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