vba Target.count 导致溢出错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25756467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Target.count causing an Overflow error
提问by DaveyD
I have Worksheet_SelectionChange
function. In the first line I wanted to condition that if more than 1 cell is selected then exit.
I wrote:
我有Worksheet_SelectionChange
功能。在第一行中,我想规定如果选择了 1 个以上的单元格,则退出。我写:
If Target.Cells.Count > 1 Then Exit Sub
However, when I select the whole worksheet, I get an error message: "Run time error 6 - Overflow"
但是,当我选择整个工作表时,我收到一条错误消息:“运行时错误 6 - 溢出”
It seems like Target.Count
can't handle such large numbers ?
好像Target.Count
不能处理这么大的数字?
What can I do to get around this?
我能做些什么来解决这个问题?
回答by Jean-Fran?ois Corbett
Replace Count
with CountLarge
.
替换Count
为CountLarge
。
Documentation: http://msdn.microsoft.com/en-us/library/office/ff196838(v=office.15).aspx
文档:http: //msdn.microsoft.com/en-us/library/office/ff196838(v=office.15).aspx
The
CountLarge
property is functionally the same as theCount
property, except that theCount
property will generate an overflow error if the specified range has more than 2,147,483,647 cells (one less than 2048 columns). TheCountLarge
property, however, can handle ranges up to the maximum size for a worksheet, which is 17,179,869,184 cells.
该
CountLarge
属性在功能上与该Count
属性相同,只是Count
如果指定范围内的单元格超过 2,147,483,647 个(少于 2048 列),该属性将产生溢出错误。CountLarge
但是,该属性最多可以处理工作表的最大大小范围,即 17,179,869,184 个单元格。
回答by teylyn
Break the check into rows and columns. This way, the count encounters a maximum of "only" 1,048,576 (rows) as opposed to 17,142,120,448 cells.
将检查分成行和列。这样,计数遇到最多“仅”1,048,576(行)而不是 17,142,120,448 个单元格。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Rows.Count > 1 Then Exit Sub
If Target.Columns.Count > 1 Then Exit Sub
' do stuff
End Sub