我需要一个可以在其他数据之间扩展范围的公式。
您可以在B列的每个月中输入Foo
或Bar
作为值。
我需要计算每个月的foo和bar数量。而且还可以将范围从B6:B11扩展到例如B6:15,并且计数功能应该起作用。
=COUNTIF(B6:B11,"Foo")
但这不是动态的。
我知道您可以使用OFFSET和COUNTA进行动态范围设置,但这是行不通的,因为数据不是按照此公式需要的方式进行结构化的。
OFFSET($A$1,0,0,COUNTA($A:$A),1)
我制作了一个VBA UDF,它可以模拟CTRL + DOWN来查找下一个填充的单元格,但是由于Foos
或Bars
的计数后来链接到另一个主工作簿,因此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一个动态范围,就在数据之间,如图所示。
如果需要帮助列,那么这不是问题。
答案 0 :(得分:2)