Highlight the max number of consecutive positive numbers

excel_1

New Member
Joined
Jul 12, 2021
Messages
5
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Platform
  1. Windows
Trying to Highlight the max number of consecutive positive numbers.

enter image description here

with the help of the following formula, can able to get the max number of consecutive positive numbers. but when tried to be applied as a conditional formula - no luck.

{=MAX(FREQUENCY(IF(A2:A20>0,ROW(A2:A20)),IF(A2:A20<=0,ROW(A2:A20))))}
 

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
Welcome to the MrExcel forum!

Try:

Book1
ABC
1Number listNumber list5
2109109
33434
4-221-221
5-561-561
6281281
731203120
8710710
94545
103232
11-12-12
128080
135656
14-231-231
15-4671-4671
16-60-60
17-16-16
18-42-42
19-71-71
205555
21
Sheet7
Cell Formulas
RangeFormula
C1C1=MAX(FREQUENCY(IF(A2:A20>0,ROW(A2:A20)),IF(A2:A20<=0,ROW(A2:A20))))
Press CTRL+SHIFT+ENTER to enter array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B:BExpression=AGGREGATE(14,6,COUNTIF(OFFSET(B1,ROW(INDIRECT("1:"&C$1))-C$1,0,C$1),">0"),1)=C$1textNO
A:AExpression=MAX(COUNTIF(OFFSET(A1,SEQUENCE(C$1,,0,-1),0,C$1),">0"))=C$1textNO


The A column uses SEQUENCE, available in Excel 365. The B column uses AGGREGATE, available in Excel 2010 and later.
 
Upvote 0
Solution
wow, that's awesome. no words but indeed it's an immense help.
 
Upvote 0
just curious, if the same thing can be achieved via VBA.
 
Upvote 0
Sure, here's one way:

VBA Code:
Sub HiLiteStreak()
Dim MaxStreak As Long, MyData As Variant, i As Long, j As Long

    Range("A:A").Interior.Color = xlNone
    MaxStreak = Evaluate("=MAX(FREQUENCY(IF(A2:A20>0,ROW(A2:A20)),IF(A2:A20<=0,ROW(A2:A20))))")
    
    MyData = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
    For i = 1 To UBound(MyData) - MaxStreak + 1
        For j = i To i + MaxStreak - 1
            If MyData(j, 1) <= 0 Then Exit For
        Next j
        If j >= i + MaxStreak Then
            Cells(i + 1, "A").Resize(MaxStreak).Interior.Color = vbCyan
            Exit Sub
        End If
        i = j
    Next i
    
End Sub
 
Upvote 0
I don't know if it is possible with your data but if there is more than one section with equal maximum positive streak then Eric's code will only highlight the first one. Removing the 'Exit Sub' line though should allow for all maximum consecutive groups to be highlighted.

I also note that one part of Eric's code is only looking at A2:A20 but another part is looking for wherever the data ends in column A. That may also need tidying up.

.. or here is another possible approach to the highlighting.
(I have assumed at least 1 positive value in column A)

VBA Code:
Sub HiLiteStreak_v2()
  Dim MaxStreak As Long
  Dim rA As Range
  
  Application.ScreenUpdating = False
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Interior.Color = xlNone
    MaxStreak = Evaluate(Replace("=MAX(FREQUENCY(IF(#>0,ROW(#)),IF(#<=0,ROW(#))))", "#", .Address))
    .Offset(-1).Resize(.Rows.Count + 1).AutoFilter Field:=1, Criteria1:=">0"
    For Each rA In .SpecialCells(xlVisible).Areas
      If rA.Rows.Count = MaxStreak Then rA.Interior.Color = vbCyan
    Next rA
    .Parent.AutoFilterMode = False
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,445
Messages
6,172,176
Members
452,446
Latest member
walkman99

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