Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

cells.count fails in Excel 2007

Latest post Fri, Feb 8 2008 2:49 PM by Mike Alexander. 12 replies.
  • Thu, Feb 7 2008 11:46 AM

    • zfraile
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Posts 38
    • Points 814

    cells.count fails in Excel 2007

    Quite simply, create a new xlsm workbook and then type ?cells.count into the immediate window in the VBE to get an overflow error.

    I guess the count property is typed as a long, which is too small to contain the value produced by 1,048,576 * 16,384 of 17,179,869,184.  That issue can probably be worked around in this case, but the long data type boundary could be an issue for a more specific range cell count if you are using a large enough range.

    What's I find particularly aggravating is that the reason I am getting this silly error is because I tried to be proactive over the last couple of years and write my code such that it would be future-proof against the new worksheet size.  So instead of writing Range("A2:A65536").EntireRow.Delete, I used Range("A2:A" & cells(cells.count).row).delete.  In theory, that would return 65,536 in an .xls file and 1,048,576 in an .xlsm file.  Range("A2:A" & rows(rows.count).row).delete does the trick, but that doesn't much help me now...

    Side note:  the help system really sucks in Excel.  I wasted a good ten minutes trying to find that data types chart that I know is in the help system.  And that's not ten minutes before I found it--that's ten minutes before I gave up and googled it. 

    • Post Points: 21
  • Thu, Feb 7 2008 12:05 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 543
    • Points 9,287
    • MVP

    Re: cells.count fails in Excel 2007

    zfraile

    And that is why they have cells.countlarge in Excel 2007

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    Filed under:
    • Post Points: 21
  • Thu, Feb 7 2008 12:26 PM In reply to

    • hrlngrv
    • Top 25 Contributor
    • Joined on Thu, Jan 17 2008
    • somewhere in the western US
    • Posts 68
    • Points 1,108

    Re: cells.count fails in Excel 2007

    Nick Hodge:

    And that is why they have cells.countlarge in Excel 2007

    And a fine example of questionable design it is! Code that assigned cells.Count to a variable of type Long would die in the new grid, so that was going to be a migration issue no matter what. That so, why not change the existing .Count property to return a Double all the time? When it's used for a range in a worksheet in a .XLS file, it presumably would return a value within the domain of the Long integer type, and VBA has no trouble converting smallish Doubles to Longs, but it could also return what .CountLarge now does when necessary.

    What useful purpose is served by having both .Count and .CountLarge properties of the Range class?

    • Post Points: 37
  • Thu, Feb 7 2008 12:32 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 543
    • Points 9,287
    • MVP

    Re: cells.count fails in Excel 2007

    And equally I suspect there were some people who didn't want the 'types' of their legacy code, run under 2007 messed with. In 2003 I had a long, in 2007 I have a double masquerading as a long.

    Short answer, I guess you could look at it either way. (Cells.Countlarge is just a find and replace after all)

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    • Post Points: 5
  • Thu, Feb 7 2008 12:34 PM In reply to

    Re: cells.count fails in Excel 2007

    What useful purpose is served by having both .Count and .CountLarge properties of the Range class

    I suspect there are a myriad of reasons for having both (backwards compatibilty, memory issues, etc...)

    After all, why have integer, long, double, etc...

     

     

    Mike Alexander Microsoft Excel MVP www.datapigtechnologies.com
    • Post Points: 21
  • Thu, Feb 7 2008 2:08 PM In reply to

    • hrlngrv
    • Top 25 Contributor
    • Joined on Thu, Jan 17 2008
    • somewhere in the western US
    • Posts 68
    • Points 1,108

    Re: cells.count fails in Excel 2007

    Mike Alexander:

    What useful purpose is served by having both .Count and .CountLarge properties of the Range class

    I suspect there are a myriad of reasons for having both (backwards compatibilty, memory issues, etc...)

    What backwards compatibility issues?

    Try to come up with a scenario in which it NECESSARY to have both .Count and .CountLarge properties.

    1. Loading .XLS files in older versions.
    2. Loading .XLS{M|B} files in older versions.
    3. Loading .XLS files in Excel 2007 and later versions.
    4. Loading .XLS{M|B} files in Excel 2007 and later versions.

    1 and 2 can't use .CountLarge anyway, and 2 would require nontrivial manual conversion if any worksheets use more than 65536 rows or 256 columns. Anyway, .CountLarge is a nonissue. As for .Count, assigning it to a Double would use 4 additional bytes of memory.

    3 could be trouble if Excel 2007 assumes every worksheet in any file uses the big grid, but if it assumes .XLS files use the 65536-by-256 grid, then no problem assigning .Count to a Long.

    4 needs to return values that exceed Long's domain. But if .Count returned a Double rather than a Long, that problem disappears.

    Other than using 4 more bytes per variable, try to think of anything that won't work the same if .Count returned a Double rather than a Long. If you don't need your code to work under Excel 2007, nothing to do anyway. If you do need your code to work under both Excel 2003 and Excel 2007, you now have to add conditional compilation blocks to assign cells.Count to variables under Excel 2003 and cells.CountLarge to variables under Excel 2007. It'd be easier to assign both properties to the same variables, and to accomodate .CountLarge, those variables would need to be type Double anyway. At that point, it would have been easier for VBA programmers if Microsoft had made .Count return a Double and dispense with .CountLarge

    Try to think of a backwards compatibility issue. Existing code assigning .Count to anything is ALREADY BROKEN because .Count could easily overflow in Excel 2007. Fixing this requires BOTH changing .Count to .CountLarge AND possibly (probably) changing the type of the variable to which one had assigned .Count and would now assign .CountLarge from Long to Double. That is, fixing the code already requires more than just switching from .Count to .CountLarge. And once you need to change the type of the variable, what purpose is served by leaving .Count as-is rather than making it return a Double?

    For those of you who still don't get it,

    Existing Excel 2003 code
    Dim n As Long
    ':
    n = ActiveSheet.UsedRange.Cells.Count

    Current Excel 2007 recoding - 1st alternative (necessary if code would be used in both 2003 and 2007)
    #If Excel2007 Then
      Dim n As Double
    #Else
      Dim n As Long
    #End If
    ':
    #If Excel2007 Then
      n = ActiveSheet.UsedRange.Cells.CountLarge
    #Else
      n = ActiveSheet.UsedRange.Cells.Count
    #End If

    Current Excel 2007 recoding - 2nd alternative (easiest if code would only be used in 2007)
    Dim n As Double
    ':
    n = ActiveSheet.UsedRange.Cells.CountLarge
     

    Alternative Excel 2007 recoding (if MSFT had done it right - code could have been used in all versions)
    Dim n As Double
    ':
    n = ActiveSheet.UsedRange.Cells.Count

    Those of you who still don't get it (i.e., understand what I mean), why not try something different and think about this issue?

    Mike Alexander:

    After all, why have integer, long, double, etc...

    Much easier to answer. Just look at the conditional compilation syntax, using # as initial character. Just like C. And all of C's numeric types, even down to calling short integers Short and long integers Long. Just a guess, but it looks like the C programmers who wrote VBA in the first place included all of C's types (even char though in VBA it's called Byte).

    Now, if you can't figure out on you own why there are separate integer (Short, Integer, Long) and floating point (Single, Double) types, I'm not going to tell you. You'll have to figure that out on your own.

    • Post Points: 37
  • Thu, Feb 7 2008 2:44 PM In reply to

    • zfraile
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Posts 38
    • Points 814

    Re: cells.count fails in Excel 2007

    I think Mike's point is clearer in light of the fact that integer data types are converted to longs anyway, so there is no reason to ever use them.

    I agree and disagree with you on the countlarge issue.  While it would have made more sense to just redefine the function of count, I'm not a big fan of existing properties and methods being changed around.  But then again, I don't think a switch in return type from long to double is a major change.  It certainly would've saved me a small headache.  Plus, you basically have to use countlarge in all cases since you're often not sure how large of a range will be returned, so why bother with the dual properties?

    Regardless, mostly I'm just glad to see that the object model has been udpated at all.

    • Post Points: 21
  • Thu, Feb 7 2008 3:21 PM In reply to

    • hrlngrv
    • Top 25 Contributor
    • Joined on Thu, Jan 17 2008
    • somewhere in the western US
    • Posts 68
    • Points 1,108

    Re: cells.count fails in Excel 2007

    zfraile:

    I think Mike's point is clearer in light of the fact that integer data types are converted to longs anyway, so there is no reason to ever use them.

    Who says Integer type variables are converted to Long type? At least in Excel 2003, the following code throws an overflow runtime error.

    Sub foo()
      Dim i As Integer, k As Long
      i = 2 ^ 15 - 1
      MsgBox i
      k = i + 1
      MsgBox k
    End Sub

    If i were implicitly converted to Long, the i + 1 expression wouldn't overflow. 

    zfraile:

    I agree and disagree with you on the countlarge issue.  While it would have made more sense to just redefine the function of count, I'm not a big fan of existing properties and methods being changed around.  But then again, I don't think a switch in return type from long to double is a major change.  It certainly would've saved me a small headache.  Plus, you basically have to use countlarge in all cases since you're often not sure how large of a range will be returned, so why bother with the dual properties?

    Yes, why bother with two properties?

    To repeat, if you're only going to use your code in Excel 2003 or earlier, not an issue - there's no .CountLarge, so nothing to change. If you're only going to use your code in Excel 2007or later, simple code update: replace .Count with .CountLarge calls and change the type of any variable to which .CountLarge is assigned to Double. But it gets messy when code would be used in both Excel 2003 and Excel 2007 (kinda makes me wonder whether this was intentional, but I'm a natural cynic) because then you need conditional compilation blocks to accomodate both .Count and .CountLarge. It would have been simpler to change .Count's return type to Double and change variable type to Double for all variables to which .Count would be assigned. Then the exact same code could have been used in both Excel 2003 and earlier and Excel 2007 and later.

    To me, the only reason for .CountLarge is to make it more difficult to use the same VBA code in all versions that support VBA. IOW, to add more preasure to upgrade because (again!) more code would need to change than just replacing .Count with .CountLarge, so Excel 2007 already requires code changes. So Microsoft wasn't sparing Excel developers that by adding .CountLarge. Rather, they were making it more difficult to use the same code in the latest version and earlier versions. Who does that help?

    • Post Points: 21
  • Thu, Feb 7 2008 4:13 PM In reply to

    Re: cells.count fails in Excel 2007

    Those of you who still don't get it (i.e., understand what I mean)...
     
    Take it easy man.  It's not that serious....have a beer.Beer 

    What backwards compatibility issues?

    Heck I don't know.  There may not be a scenario where it's necessary to have both .count and .countlarge, but I do know that when you have 400 million + users using your product, you don't cavalierly start changing the way core properties work. 

     

     

     

    Mike Alexander Microsoft Excel MVP www.datapigtechnologies.com
    • Post Points: 21
  • Thu, Feb 7 2008 4:51 PM In reply to

    • hrlngrv
    • Top 25 Contributor
    • Joined on Thu, Jan 17 2008
    • somewhere in the western US
    • Posts 68
    • Points 1,108

    Re: cells.count fails in Excel 2007

    Mike Alexander:

    ...
    What backwards compatibility issues?

    Heck I don't know. There may not be a scenario where it's necessary to have both .count and .countlarge, . . .

    Try to think of one. Just one. In the context that if you're using Excel 2007 you already have to rewite code, replacing .Count property calls with .CountLarge and probably have to change the type of the variable(s) to which it's assigned.

    Mike Alexander:

    . . . but I do know that when you have 400 million + users using your product, you don't cavalierly start changing the way core properties work.

    But you do cavalierly change the entire UI?

    Note that Microsoft did change the way various properties work (e.g., Application.StatusBar), and the bigger grid requires at least checking all code and likely rewriting a significant bit of it.

    Still knee-jerk responses. 

    • Post Points: 5
  • Thu, Feb 7 2008 5:46 PM In reply to

    • zfraile
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Posts 38
    • Points 814

    Re: cells.count fails in Excel 2007

    hrlngrv:
    Who says Integer type variables are converted to Long type?

     

    That would be Harald.  This is what I'm referring to: 

    http://www.dailydoseofexcel.com/archives/2004/08/27/long-vs-integer/ 

    • Post Points: 21
  • Thu, Feb 7 2008 8:23 PM In reply to

    Re: cells.count fails in Excel 2007

    zfraile:

    hrlngrv:
    Who says Integer type variables are converted to Long type?

    That would be Harald.  This is what I'm referring to: 

    http://www.dailydoseofexcel.com/archives/2004/08/27/long-vs-integer/

    which refers to a newsgroup posting which refers to

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/decontheintegerdatatypes.asp

    which may be technically accurate about how Integers are stored as Longs, but they're used as [short] Integers. If so, there's no good reason to use Integers.

    • Post Points: 21
  • Fri, Feb 8 2008 2:49 PM In reply to

    Re: cells.count fails in Excel 2007

    On a tangential note, here's a general piece of advice from Professional Excel Development, Chapter 17 (Optimizing VBA Performance). 

    Paraphrased quote:

    "When passing numbers to Excel...it is usually most efficient to pass variables declared As Double. 
    This is because Excel generally uses the Double data type internally and so avoids type conversions.
    Using Double also avoids Excel's autoformatting and so improves performance."

     

    Mike Alexander Microsoft Excel MVP www.datapigtechnologies.com
    • Post Points: 5
Page 1 of 1 (13 items) | RSS
Copyright Excel User Group and the relevant contributors, 2010. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.