【发布时间】:2019-06-05 05:57:19
【问题描述】:
我需要一个可以在其他数据之间扩展范围的公式。
在每个月的 B 列中,您可以将 Foo 或 Bar 作为值。
我需要计算每个月的 foos 和 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 一个动态范围,如图所示。
如果需要辅助列,那么这不是问题。
【问题讨论】:
标签: excel excel-formula