数据之间的公式动态范围

时间:2019-06-05 05:57:19

标签: excel excel-formula

我需要一个可以在其他数据之间扩展范围的公式。
您可以在B列的每个月中输入FooBar作为值。
我需要计算每个月的foo和bar数量。而且还可以将范围从B6:B11扩展到例如B6:15,并且计数功能应该起作用。

enter image description here

=COUNTIF(B6:B11,"Foo")

但这不是动态的。
我知道您可以使用OFFSET和COUNTA进行动态范围设置,但这是行不通的,因为数据不是按照此公式需要的方式进行结构化的。

OFFSET($A$1,0,0,COUNTA($A:$A),1)

我制作了一个VBA UDF,它可以模拟CTRL + DOWN来查找下一个填充的单元格,但是由于FoosBars的计数后来链接到另一个主工作簿,因此UDF将不起作用,UDF不能与链接值一起运行。 (据我所知)

=COUNTIF(INDIRECT("B"&nextC(A5)&":B"&last(A5)),"Foo")

nextC和last在哪里:

Function last(rng)
    last = Sheets("Sheet1").Cells(rng.Row, "A").End(xlDown).Row - 1 ' finds the last cell row in the current month
    ' with A5 as input it returns 11
End Function

Function nextC(rng)
    nextC = rng.Offset(1, 0).Row ' returns 6 if input is A5 
    ' there may be a better way to do this, I just couldn't think of it at the moment and just wanted to see if it worked
End Function

是否存在任何可以复制UDF的公式,这意味着给COUNTIF一个动态范围,就在数据之间,如图所示。
如果需要帮助列,那么这不是问题。

1 个答案:

答案 0 :(得分:2)

这应该可以解决问题:

enter image description here

D2中的公式:

=COUNTIF(INDEX($B:$B,MATCH($C$1,$A$1:$A$1000,0)+1):INDEX($B:$B,MATCH(FALSE,INDEX($A:$A,MATCH($C$1,$A$1:$A$1000,0)+2):$A$1000="",0)+MATCH($C$1,$A$1:$A$1000,0)),D1)

通过 Ctrl Shift Enter

输入

并拖动到E2 ....

添加新行时的结果:

enter image description here

A1000范围更改为您需要的大小。它只是在确保您超过最后一行,我在其中放置了一个字符串“ End”以标记最后一行。