Longest Streak Of Visible Cells

honkin

Active Member
Joined
Mar 20, 2012
Messages
374
Office Version
  1. 2016
Platform
  1. MacOS
I have a formula which calculates longest streak of the number 1 in column AC, but I have just realised that the sheet will quite often be autofiltered

This is the formula

{=MAX(FREQUENCY(IF(AC:AC=1,ROW(AC:AC)),IF(AC:AC<>1,ROW(AC:AC))))}

It is an array formula, but it does not handle the results correctly when the sheet is filtered

Any idea of the change/s required to have it work only on visible cells?

cheers
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try the following formula, which needs to be confirmed with CONTROL+SHIFT+ENTER...

=MAX(FREQUENCY(IF(SUBTOTAL(3,OFFSET(AC2:AC100,ROW(AC2:AC100)-ROW(AC2),0,1))>0,IF(AC2:AC100=1,ROW(AC2:AC100))),IF(AC2:AC100<>1,ROW(AC2:AC100))))

...and adjust the range accordingly. I would suggest that you avoid whole column references, since it's very inefficient. Also, to exclude manually hidden rows from the calculation, change SUBTOTAL(3, . . . ) TO SUBTOTAL(103, . . . ).

Hope this helps!
 
Upvote 0
cheers Domenic and thanks for your reply

The above formula does seem to accurately calculate the number of consecutive 1s in column AC. I changed subtotal 3 to 103 so it handled manually hidden rows, but still it is not being accurate at all. Here is an example below with the cell I have the formula in showing the consecutive streak of 1s as 1 rather than 2

2021 Latest Results June 2021.xlsb
AC
14Finish
15Position
3271
9471
rc-vdw-place-adapted-2021-01-01


This is the full formula now

{=MAX(FREQUENCY(IF(SUBTOTAL(103,OFFSET(AC16:AC1048576,ROW(AC16:AC1048576)ROW(AC16),0,1))>0,IF(AC16:AC1048576=1,ROW(AC16:AC1048576))),IF(AC16:AC1048576<>1,ROW(AC16:AC1048576))))}

Any thoughts Domenic?

cheers
 
Upvote 0
It looks like you have a typo. You're missing a minus ( - ) sign...

VBA Code:
=MAX(FREQUENCY(IF(SUBTOTAL(103,OFFSET(AC16:AC1048576,ROW(AC16:AC1048576)-ROW(AC16),0,1))>0,IF(AC16:AC1048576=1,ROW(AC16:AC1048576))),IF(AC16:AC1048576<>1,ROW(AC16:AC1048576))))
 
Upvote 0
It looks like you have a typo. You're missing a minus ( - ) sign...

VBA Code:
=MAX(FREQUENCY(IF(SUBTOTAL(103,OFFSET(AC16:AC1048576,ROW(AC16:AC1048576)-ROW(AC16),0,1))>0,IF(AC16:AC1048576=1,ROW(AC16:AC1048576))),IF(AC16:AC1048576<>1,ROW(AC16:AC1048576))))
Cheers again Domenic

Strange the minus sign disappeared, as I merely did a copy and past and altered the range.

Ah I know what it was. The minus was there, but when I copied it here, it went to 2 lines, right at the minus. When I backspaced to get it on a single line, I obviously deleted it, but only here on the forum.

Still no good with this code, unfortunately. Here is a sample

2021 Latest Results June 2021.xlsb
AC
14Finish
15Position
194
212
431
1241
2041
2353
3271
4451
5006
5032
rc-vdw-place-adapted-2021-01-01


The result cell shows the number of consecutive 1s as 1, not 3

Here is the formula

{=MAX(FREQUENCY(IF(SUBTOTAL(103,OFFSET(AC16:AC1048576,ROW(AC16:AC1048576)-ROW(AC16),0,1))>0,IF(AC16:AC1048576=1,ROW(AC16:AC1048576))),IF(AC16:AC1048576<>1,ROW(AC16:AC1048576))))}

Any thoughts as to what may be stopping it from working correctly?

cheers
 
Upvote 0
Sorry, my mistake, we have to add the same subtotal criteria for the bins array...

VBA Code:
=MAX(FREQUENCY(IF(SUBTOTAL(103,OFFSET(AC16:AC1048576,ROW(AC16:AC1048576)-ROW(AC16),0,1))>0,IF(AC16:AC1048576=1,ROW(AC16:AC1048576))),IF(SUBTOTAL(103,OFFSET(AC16:AC1048576,ROW(AC16:AC1048576)-ROW(AC16),0,1))>0,IF(AC16:AC1048576<>1,ROW(AC16:AC1048576)))))

...confirmed with CONTROL+SHIFT+ENTER.
 
Last edited:
Upvote 0
Solution
Apologies for the long delay, Domenic. This seems to work very well, so thank you very much

cheers
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,656
Members
449,045
Latest member
Marcus05

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