简体   繁体   English

Selection.SpecialCells(xlCellTypeVisible).Count 在过滤器产生单行结果时给出溢出错误

[英]Selection.SpecialCells(xlCellTypeVisible).Count gives overflow error when single row result from filter

When using Selection.SpecialCells(xlCellTypeVisible).Count to count a single column of filtered data, it works great for multiple row results.当使用Selection.SpecialCells(xlCellTypeVisible).Count计算单列过滤数据时,它对多行结果非常有用。 But when ever there is only 1 row displayed, it gives me an overflow error or I get count = 107564 (I forget the actual number).但是当只显示 1 行时,它会给我一个溢出错误或者我得到 count = 107564(我忘记了实际数字)。

.Count is of type Long but newer Excel versions have more cells per worksheet ( 17,179,869,184 Cells) than Long can handle (maximum 2,147,483,647 ). .CountLong类型,但较新的 Excel 版本每个工作表的单元格( 17,179,869,184单元格)多于Long可以处理的(最多2,147,483,647个)。 So if you select a huge amount ouf cells they exceed Long and therefore you get an overflow error.因此,如果您 select 大量 ouf 单元格,它们会超过Long ,因此您会收到溢出错误。

To solve this you need to use the Range.CountLarge property instead of the Range.Count property which is of type LongLong and can handle this amount of cells.要解决此问题,您需要使用Range.CountLarge 属性而不是LongLong类型的Range.Count 属性,并且可以处理此数量的单元格。

Data type数据类型 Storage size存储空间 Range范围
Long (Long integer) Long(长整型) 4 bytes 4字节 -2,147,483,648 to 2,147,483,647 -2,147,483,648 至 2,147,483,647
LongLong (LongLong integer) LongLong(LongLong 整数) 8 bytes 8字节 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (Valid on 64-bit platforms only.) -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807(仅在 64 位平台上有效。)

Table source: Office VBA Reference - Data type summary .表格来源: Office VBA 参考 - 数据类型摘要

This should not throw an overflow error:这不应该引发溢出错误:

Selection.SpecialCells(xlCellTypeVisible).CountLarge

There is a simple rule: When it comes to counting rows or columns .Count is fine but everytime you are counting cells (in multiple rows or columns) you need to ensure to use .CountLarge to be safe.有一个简单的规则:当涉及到对行或列进行计数时, .Count很好,但每次您对单元格(在多行或多列中)进行计数时,您都需要确保使用.CountLarge以确保安全。

It seems that if you select only 1 cell then excel works out all visible cells in the sheet.似乎如果您 select 只有 1 个单元格,那么 excel 会计算出工作表中的所有可见单元格。 Selecting one cell and running this line:选择一个单元格并运行此行:

Debug.Print Selection.SpecialCells(xlCellTypeVisible).Address

gave me $1:$13,$15:$17,$19:$19,$28:$37,$39:$39,$41:$52,$54:$81,$83:$1048576 .给了我$1:$13,$15:$17,$19:$19,$28:$37,$39:$39,$41:$52,$54:$81,$83:$1048576 Counting the number of cells in this range using .Count results in an overflow error使用.Count计算此范围内的单元格数会导致溢出错误

This is obviously not the behaviour you want.这显然不是您想要的行为。 As a workaround try something like this:作为解决方法尝试这样的事情:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Selection.SpecialCells(xlCellTypeVisible)时未选中的单元会受到影响 - Non selected cells being affected when using Selection.SpecialCells(xlCellTypeVisible)' 如何在使用 Selection.SpecialCells(xlCellTypeVisible) 时检测何时未选择任何单元格? - How to detect when no cell is selected while using Selection.SpecialCells(xlCellTypeVisible)? SpecialCells(xlCellTypeVisible)-自动过滤器返回零行时出错 - SpecialCells(xlCellTypeVisible) - Error when Autofilter returns zero rows 我的 VBA Selection.SpecialCells(xlCellTypeConstants, 1) cell.Value = cell.Text 中的错误 - Error in my VBA Selection.SpecialCells(xlCellTypeConstants, 1) cell.Value = cell.Text SpecialCells(xlCellTypeVisible) - SpecialCells(xlCellTypeVisible) 在 excel vba 中选择了 SpecialCells(xlCellTypeVisible) 额外行 - SpecialCells(xlCellTypeVisible) extra row is selected in excel vba .SpecialCells(xlCellTypeVisible).Rows.Count 不会返回到正确的可见行数 - .SpecialCells(xlCellTypeVisible).Rows.Count does not return to a correct visible row count Selection.SpecialCells() 方法返回意外范围(Excel VBA) - Selection.SpecialCells() method returns unexpected range (Excel VBA) AutoFilter返回正确的结果,但是当SpecialCells(xlCellTypeVisible).value时,excel仅返回记录的一半 - AutoFilter return correct result, but when SpecialCells(xlCellTypeVisible).value , excel only return half of the record 遍历过滤器中的枢轴项时,vba SpecialCells(xlCellTypeVisible)无法正常工作 - vba SpecialCells(xlCellTypeVisible) not working correctly when looping through pivot items in a filter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM