[英]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
). .Count
是Long
类型,但较新的 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.