Counting consecutive duplicates of identical values in chaotic order

thomas819

New Member
Joined
Nov 23, 2020
Messages
36
Office Version
  1. 2019
Platform
  1. Windows
Hello all,
I would like to ask you for help. I am currently facing very interesting challenge and unfortunately I can't solve it alone.

In this table you can see list of 5 products with error description.
ProductError description
1​
error0
1​
no error
2​
error1
2​
erroe4
3​
no error
4​
error2
5​
error2
4​
no error
4​
no error
5​
no error
5​
no error
5​
no error
1​
error5
5​
no error
1​
no error
4​
error2
4​
no error
1​
no error
3​
no error
3​
no error
5​
error4
1​
no error
4​
no error
1​
no error
3​
no error
4​
error6
5​
no error
5​
no error

Result should like this:
I need to find out how many times we got "no error" without interruption in row in last occurrence of an particular product.
If we have have as last record "error" then counter should be reset.

Actual result I am looking for:

1​
no error4x
2​
no error0x
3​
no error4x
4​
no error0x
5​
no error2x

Thank you very much for any help you may provide, because I can't move forward at all.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Does this help?

22 10 02.xlsm
ABCDE
1ProductError descriptionProductCount
21error014
31no error20
42error134
52erroe440
63no error52
74error2
85error2
94no error
104no error
115no error
125no error
135no error
141error5
155no error
161no error
174error2
184no error
191no error
203no error
213no error
225error4
231no error
244no error
251no error
263no error
274error6
285no error
295no error
Count
Cell Formulas
RangeFormula
E2:E6E2=LEN(TRIM(RIGHT(SUBSTITUTE(CONCAT(IF(A$2:A$29=D2,IF(B$2:B$29="no error",1," "),""))&1," ",REPT(" ",999)),999)))-1
 
Upvote 0
Solution
Does this help?

22 10 02.xlsm
ABCDE
1ProductError descriptionProductCount
21error014
31no error20
42error134
52erroe440
63no error52
74error2
85error2
94no error
104no error
115no error
125no error
135no error
141error5
155no error
161no error
174error2
184no error
191no error
203no error
213no error
225error4
231no error
244no error
251no error
263no error
274error6
285no error
295no error
Count
Cell Formulas
RangeFormula
E2:E6E2=LEN(TRIM(RIGHT(SUBSTITUTE(CONCAT(IF(A$2:A$29=D2,IF(B$2:B$29="no error",1," "),""))&1," ",REPT(" ",999)),999)))-1
Hi Peter,
you are magic. Thank you very much .
To be honest I never even concider LEN nor TRIM functions to solve this issue. My approach was through FREQUENCY function...

Thank you :)
 
Upvote 0
You are very welcome. My first thought also was related to FREQUENCY but I ended up thinking that TRIM(RIGHT( ...)) was an easier way to check the 'last' results. :)
 
Upvote 0
You are very welcome. My first thought also was related to FREQUENCY but I ended up thinking that TRIM(RIGHT( ...)) was an easier way to check the 'last' results. :)
Hi Peter, I would like to ask you for other favour.
Can you somehow tweak your formula... because I have to run it using Office 2016 and CONCAT function is not supported.

At home I have Office 2019 and here is no problem running that function, but under Office 2016 is other story.

Thank you in advance :)
 
Upvote 0
You could try this user-defined function.

VBA Code:
Function MyCount(rData As Range, sProd As String, sDesc As String) As Long
  Dim a As Variant
  Dim i As Long
  
  a = rData.Value
  i = UBound(a)
  Do Until (a(i, 1) = sProd And LCase(a(i, 2)) <> LCase(sDesc)) Or i = 1
    If a(i, 1) = sProd And LCase(a(i, 2)) = LCase(sDesc) Then MyCount = MyCount + 1
    i = i - 1
  Loop
End Function

Note that the data range fed into the function includes the heading row.

thomas819.xlsm
ABCDE
1ProductError descriptionProductCount
21error014
31no error20
42error134
52erroe440
63no error52
74error2
85error2
94no error
104no error
115no error
125no error
135no error
141error5
155no error
161no error
174error2
184no error
191no error
203no error
213no error
225error4
231no error
244no error
251no error
263no error
274error6
285no error
295no error
Count
Cell Formulas
RangeFormula
E2:E6E2=MyCount(A$1:B$29,D2,"no error")
 
Upvote 0
You could try this user-defined function.

VBA Code:
Function MyCount(rData As Range, sProd As String, sDesc As String) As Long
  Dim a As Variant
  Dim i As Long
 
  a = rData.Value
  i = UBound(a)
  Do Until (a(i, 1) = sProd And LCase(a(i, 2)) <> LCase(sDesc)) Or i = 1
    If a(i, 1) = sProd And LCase(a(i, 2)) = LCase(sDesc) Then MyCount = MyCount + 1
    i = i - 1
  Loop
End Function

Note that the data range fed into the function includes the heading row.

thomas819.xlsm
ABCDE
1ProductError descriptionProductCount
21error014
31no error20
42error134
52erroe440
63no error52
74error2
85error2
94no error
104no error
115no error
125no error
135no error
141error5
155no error
161no error
174error2
184no error
191no error
203no error
213no error
225error4
231no error
244no error
251no error
263no error
274error6
285no error
295no error
Count
Cell Formulas
RangeFormula
E2:E6E2=MyCount(A$1:B$29,D2,"no error")
Hi Peter,
thank you once more. You are trully excelent. I also tried this (custom function) approach, but it was running very long. I am talkingh about tens of seconds.

Thank you very much :)
 
Upvote 0
I also tried this (custom function) approach, but it was running very long.
Sounds like you must have a lot of these formulas in the sheet then and/or very large data ranges with products well scattered?

If all the error descriptions are lower case like your samples then changing the function to the code below may help, though I'm not sure that the difference will be much.

VBA Code:
Function MyCount(rData As Range, sProd As String, sDesc As String) As Long
  Dim a As Variant
  Dim i As Long
  
  a = rData.Value
  i = UBound(a)
  Do Until (a(i, 1) = sProd And a(i, 2) <> sDesc) Or i = 1
    If a(i, 1) = sProd Then
      If a(i, 2) = sDesc Then MyCount = MyCount + 1
    End If
    i = i - 1
  Loop
End Function
 
Upvote 0
Sounds like you must have a lot of these formulas in the sheet then and/or very large data ranges with products well scattered?

If all the error descriptions are lower case like your samples then changing the function to the code below may help, though I'm not sure that the difference will be much.

VBA Code:
Function MyCount(rData As Range, sProd As String, sDesc As String) As Long
  Dim a As Variant
  Dim i As Long
 
  a = rData.Value
  i = UBound(a)
  Do Until (a(i, 1) = sProd And a(i, 2) <> sDesc) Or i = 1
    If a(i, 1) = sProd Then
      If a(i, 2) = sDesc Then MyCount = MyCount + 1
    End If
    i = i - 1
  Loop
End Function
Hi Peter, thank you.
But you missunderstund what I meant. I tried create my own function, but since this was my first attempt to do so, it wasn't write in sufficient way and therefore vba runs over 40s.

Your original custom function run smoothly and very fast :)

Thank you.
 
Upvote 0
OK, thanks for the clarification. Glad it is working well for you. :)
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,695
Members
449,331
Latest member
smckenzie2016

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