Неверное значение при подсчете ячеек в разных версиях Excel

Я видел, что некоторые пользователи SO сталкивались с проблемой при попытке использовать некоторые вариантыCells.Count; в некоторых случаях код VBA вызывает ошибку переполнения.

Для справки смотрите комментарии кэтот ответ:

I think this will work, but I get an "overflow" error and it points me to the code "If Master.Cells.SpecialCells(xlCellTypeVisible).Count > 0 Then" --- it seems like it's not filtering for anything in particular – user1556069

а такжеэтот ответ:

Is this onyl working (and Cells.Count didnt work) because the latter used an integer, 16 bits, max value of 65,536 and the whole spreadsheet returned a numbr greater? – fast_code

I'm assuming that somewhere behind the scenes VBA is trying to coerce the cell count to a small Integer (16-bit) or Long integer (32-bit). The cell count of an Excel 2007 worksheet would overflow both of those datatypes. Unfortunately I can't isolate it right now because I don't have a copy of Excel 2007 handy and cannot actually reproduce your error. – mwolfe02

Пытаясь понять это, я пытался воспроизвести себя и получил переполнение при попытке назначитьCells.Count как целое число Это имеет смысл, так как значение слишком велико для типа данных Integer.

Используя приведенный ниже код в Excel 2003 и 2010, я получил числовой результат при попытке присвоить значение Long или Variant.

Option Explicit

Sub testInteger()
    Dim i As Integer
    i = Cells.Count 'Overflow
    Debug.Print i 'Doesn't get this far...
End Sub

Sub testLong()
    Dim l As Long
    l = Cells.Count
    Debug.Print l 'Prints 16777216 in both versions
End Sub

Sub testVariant()
    Dim v As Variant
    v = Cells.Count
    Debug.Print v 'Prints 16777216 in both versions
End Sub

Как вы можете видеть в моих комментариях,Cells.Count значение16777216 (что верно для 2003 года), но это то же самоеfor both versionsи это не имеет смысла для меня. Цитироватьmwolfe02 из одного из вышеуказанных ответов:

Excel 2007 worksheets have 1,048,576 rows and 16,384 columns for a total of 17,179,869,184 cells.

Что говорит мне, что значение, напечатанное в 2010 году, должно быть как минимум (я считаю, что оно должно быть таким же)17,179,869,184.

So why does this number not print correctly/why is the 2003 value returned in 2010?

Ответы на вопрос(1)

Ваш ответ на вопрос