Target.count causing an Overflow error

有些话、适合烂在心里 提交于 2019-11-30 17:50:20

问题


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:

    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"

It seems like Target.Count can't handle such large numbers ?

What can I do to get around this?


回答1:


Replace Count with CountLarge.

Documentation: http://msdn.microsoft.com/en-us/library/office/ff196838(v=office.15).aspx

The CountLarge property is functionally the same as the Count property, except that the Count property will generate an overflow error if the specified range has more than 2,147,483,647 cells (one less than 2048 columns). The CountLarge property, however, can handle ranges up to the maximum size for a worksheet, which is 17,179,869,184 cells.




回答2:


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.

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


来源:https://stackoverflow.com/questions/25756467/target-count-causing-an-overflow-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!