首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当筛选结果为单行时,Selection.SpecialCells(xlCellTypeVisible).Count会给出溢出错误

问当筛选结果为单行时,Selection.SpecialCells(xlCellTypeVisible).Count会给出溢出错误
EN

Stack Overflow用户
提问于 2021-06-18 13:45:46
回答 1查看 165关注 0票数 1

当使用Selection.SpecialCells(xlCellTypeVisible).Count计算单列过滤数据时,它非常适用于多行结果。但是当只有1行显示时,它会给我一个溢出错误,或者我得到count = 107564 (我忘记了实际的数字)。

EN

回答 1

Stack Overflow用户

发布于 2021-06-18 14:22:41

似乎如果您只选择一个单元格,那么excel就会计算出工作表中所有可见的单元格。选择一个单元格并运行以下代码行:

代码语言:javascript
复制
Debug.Print Selection.SpecialCells(xlCellTypeVisible).Address

给了我$1:$13,$15:$17,$19:$19,$28:$37,$39:$39,$41:$52,$54:$81,$83:$1048576。使用.Count计算此范围内的单元格数量会导致溢出错误

这显然不是你想要的行为。作为一种变通办法,可以尝试如下所示:

代码语言:javascript
复制
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 Function
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68029930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档