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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:29:19  来源:igfitidea点击:

Target.count causing an Overflow error

excelvbaexcel-vbaoverflow

提问by DaveyD

I have Worksheet_SelectionChangefunction. 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.Countcan't handle such large numbers ?

好像Target.Count不能处理这么大的数字?

What can I do to get around this?

我能做些什么来解决这个问题?

回答by Jean-Fran?ois Corbett

Replace Countwith CountLarge.

替换CountCountLarge

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 CountLargeproperty is functionally the same as the Countproperty, except that the Countproperty will generate an overflow error if the specified range has more than 2,147,483,647 cells (one less than 2048 columns). The CountLargeproperty, 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