计数连续的可见空白单元格

我有一个数据集在每列中有大量的空白字段。 我想在将一些任意filter应用到其他列之后,计算每列中空白单元格的数量。

我已经得到这个工作在一个sub与以下

  Sub whatever() Dim myrange As Range Set myrange = Worksheets("Sheet1").Range("a1:a100") myrange.SpecialCells(xlCellTypeVisible).SpecialCells(xlCellTypeBlanks).Count End Sub 

但是当我尝试像这样把它放在UDF中

  Function CountBlankVisible(myrange As Range) CountBlankVisible = myrange.SpecialCells(xlCellTypeVisible).SpecialCells(xlCellTypeBlanks).Count End Function 

它似乎在计数范围内的每个细胞,而不pipe细胞types如何。 任何想法,为什么这将工作在一个子而不是作为一个function? 有可能用其他方法来计算这个数字吗?

Excel UDF有一些限制(从工作表中调用时)。 你可以在这里阅读。

这里是工作的例子:

 Function CountBlankVisible(myrange As Range) Dim c As Range For Each c In myrange If c.RowHeight > 0 And IsEmpty(c.Value) Then _ CountBlankVisible = CountBlankVisible + 1 Next End Function 

作为simoco代码的替代:

 Function CountBlankVisible(myrange As Range) Dim c As Range For Each c In myrange If Not c.EntireRow.Hidden And c.Value ="" Then CountBlankVisible = CountBlankVisible + 1 End If Next End Function