Search code examples
ms-accessvba

Is there a constant for max value for integer type?


I'm searching for a constant like MAXINT in c, for VBA code. I found references only in other languages, and cannot find one for VBA.

If there is no such constant what is the maximum number an int in VBA can hold? I tried 2147483647 but got an overflow error.


Solution

  • VBA does not provide a MAXINT constant. But you can derive that value easily:

    MAXINT = (2 ^ 15) -1
    Debug.Print MAXINT
     32767
    

    Or you could define it as a Public constant with this in the Declarations section of a standard module:

    Public Const MAXINT As Integer = (2 ^ 15) - 1
    

    Then MAXINT would be available for the rest of your VBA code in that application.

    And for Long Integer, the maximum value is ...

    MAXLONG = (2 ^ 31) -1
    Debug.Print MAXLONG
     2147483647