公式来计算符合条件的块

我有一个过去12个月的date范围,对于每个客户的date范围是“开”或“关”值。 我基本上是希望在重新开始之前能够计算好几天他们都有好处的每个“closures”期间。

Col A 01/01/2014 02/01/2014 03/01/2014 04/01/2014 1 off off on off 2 on off off off 3 on on on on 

所以基本上在每一行date结束时,我想要另外六个列块1到块6(块6将永远是最大的)所以对于上面的ID 1closures利益两天然后再次打开和closures,所以我想最后的列看起来像:

 Col A Block 1 Block 2 Block 3 Block 4 etc etc 1 2 1 - - 2 3 - - - 3 - - - - 

每次他们回来的好处,然后回来,开始一个新的块。 有没有简单的方法来做到这一点? 我有近800k的行,真的不想手动这样做

我已经设法使用一个数组公式,我不明白UDF足够的时候使用以前的答案。

 =IFERROR(SMALL(IF(($A3:$OF3="Off")*($B3:$OG3<>"Off"),COLUMN($B3:$OG3),""),OJ$1)-SMALL(IF(($A3:$OF3<>"Off")*($B3:$OG3="Off"),COLUMN($B3:$OG3),""),OJ$1),"") 

这似乎与帮助专栏1至6

我检查了近6000行数据的结果,似乎完全没有发现任何问题。

感谢你的帮助

您可以使用以下用户定义的函数来获得此结果。

如果您对UDF不熟悉,则可以按照以下教程进行操作:

http://www.wikihow.com/Create-a-User-Defined-Function-in-Microsoft-Excel

 Function CountBlock(v As Variant) As Variant Dim output(1 To 6) As Variant Dim cell As Variant Dim count As Integer Dim i As Integer i = 1 For Each cell In v If cell = "off" Then count = count + 1 ElseIf i = 6 Then Exit For ElseIf count > 0 Then output(i) = count i = i + 1 count = 0 End If Next output(i) = count CountBlock = output End Function 

结果将作为一个数组输出,所以你会tu使用6项的“数组公式”。 只要select6个连续的单元格。 然后写=CountBlock(B2:E2)并做control+Enter