Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8330709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:11:59+00:00 2026-06-09T02:11:59+00:00

I have seen some SO users run into an issue when trying to use

  • 0

I have seen some SO users run into an issue when trying to use some variation of Cells.Count; the VBA code throws an overflow error in some cases.

For reference, see comments on this answer:

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

and this answer:

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

Trying to understand this, I tried to reproduce myself and got an overflow when trying to assign Cells.Count as an Integer. This makes sense as the value is too large for the Integer data type.

Using the code below in both Excel 2003 and 2010, I was given a numeric result when trying to assign as a Long or 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

As you can see in my comments, the Cells.Count value is 16777216 (which is correct for 2003), but it is the same for both versions, and that doesn’t make sense to me. To quote mwolfe02 from one of the above-linked answers:

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

Which tells me the value printed in 2010 should be at least (I believe it should really be the same) 17,179,869,184.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:12:02+00:00Added an answer on June 9, 2026 at 2:12 am

    When calculating such large numbers use .Countlarge property.

    For example

    Sub CellsCount()
        Dim l As Double
        l = ActiveSheet.Cells.CountLarge
        Debug.Print l
    End Sub
    

    Also never use Cells.Count or Cells.CountLarge without specifying the worksheet object. This is to ensure that we don’t get incorrect count/error in compatibility mode. Similarly never use Rows.Count. Always use ws.Rows.Count. This is the most common error people make while trying to find the last row in excel. For example

    This

    lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
    

    and

    lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    

    might not give you the same results always.

    I would also recommend further reading of this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen some instances where people will say you have to use JS
I have seen some developers use the return statement in a catch block. Why/when
I am using a WPF ribbon and I have just run into some strange
I have run into a rather weird little problem. In the following code I
Have seen some similar questions: What is the difference between a JavaBean and a
I have seen some people in SO commenting that Singleton Pattern is an anti-pattern.
I have seen some related questions but none focusing on the specific problem I
I have seen some mails which has HTML content embedded in them. The content
i have seen some hand rolled solutions but does jquery out of the box
I have seen some of the other answers on this topic but dont really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.