当使用Selection.SpecialCells(xlCellTypeVisible).Count计算单列过滤数据时,它非常适用于多行结果。但是当只有1行显示时,它会给我一个溢出错误,或者我得到count = 107564 (我忘记了实际的数字)。
发布于 2021-06-18 14:22:41
似乎如果您只选择一个单元格,那么excel就会计算出工作表中所有可见的单元格。选择一个单元格并运行以下代码行:
Debug.Print Selection.SpecialCells(xlCellTypeVisible).Address给了我$1:$13,$15:$17,$19:$19,$28:$37,$39:$39,$41:$52,$54:$81,$83:$1048576。使用.Count计算此范围内的单元格数量会导致溢出错误
这显然不是你想要的行为。作为一种变通办法,可以尝试如下所示:
Function CountVisibleCells() As Long
Dim rngSelection As Range
Set rngSelection = Selection
CountVisibleCells = 0
If rngSelection.Cells.CountLarge > 1 Then
CountVisibleCells = rngSelection.SpecialCells(xlCellTypeVisible).Cells.CountLarge
ElseIf Not rngSelection.EntireRow.Hidden And _
Not rngSelection.EntireColumn.Hidden Then
CountVisibleCells = 1
End If
End Functionhttps://stackoverflow.com/questions/68029930
复制相似问题