VBA Number Types: Integer


General information about "Integer"

From -32768 to 32767

In VBA, a number of the type Integer is a number that can contain values in {-32768,...,-1,0,1,..., 32767}.

The following VBA code works.

When you assign a value outside of the range {-32768,...,-1,0,1,..., 32767}. the VBA code will crash. At run time, it will generate an overflow error.

Hexadecimal assigment

You can use hexadecimal assignment in VBA.

No fractional values

A number of the type Integer cannot contain fractional values, like 3.1415. Be careful, because of an implicit conversion, this VBA code does work.

Default

On declaration, by default, VBA assigns the value 0 to the number of the type Integer.

Type declaration character

There is no type declaration character for Integer. If the number is in the range {-32768,...,-1,0,1,..., 32767}, the type of the number is set to Integer by default.

Two memory bytes

A number of the type Integer takes up two bytes in the memory. In a byte, there are 8 bits, each of which can take the value 0 or 1. Hence, two bytes can have 2^16 possible bit representations. 215 of them are negative, 215-1 of them are positive, 1 of them is zero. Each of these 65536 possible bit representations corresponds to one value in {-32768,...,-1,0,1,..., 32767}. One bit serves as the sign bit.

To illustrate, one possible bit representation could be as follows. The VBA bit representation might be slightly different, but the logic is the same.

BitsValue
00000000000000000
00000000000000011
00000000000000102
...
011111111111111032766
011111111111111132767
1000000000000001-1
1000000000000010-2
...
1111111111111110-32767
1111111111111111-32768

Here is how to read the bit representation. For instance, the bit representation 00110010 00110110

Bit 1sign bit0 means +, 1 means -
Bit 20 x 16384=0
Bit 31 x 8192=8192
Bit 41 x 4096=4096
Bit 50 x 2048=0
Bit 60 x 1024=0
Bit 71 x 512=512
Bit 80 x 256=0
Bit 90 x 128=0
Bit 100 x 64=0
Bit 111 x 32=32
Bit 121 x 16=16
Bit 130 x 8=0
Bit 141 x 4=4
Bit 151 x 2=2
Bit 160 x 1=0

corresponds to 12854 (= 2 + 4 + 16 + 32 + 512 + 4096 + 8192).

Doing +,-,/ etc.

You can make additions, subtractions, multiplications and so on. Numercial operations are exact, provided that you stay within the range {-32768,...,-1,0,1,...,32767}.

This VBA code will crash because the result is outside the range {-32768,..., 32767}.

Or, more subtle, and more dangerous.

Of course, division by zero is forbidden. This VBA code will also crash.

Be careful when doing numerical operations. This VBA code does not crash.

The range {-32768,...,-1,0,1,...,32767} is not very wide. Even for simple numerical operations, like augmenting a counter with 1 in a loop, the range risks being too small. If you don't think about this when coding - which is normal - it may lead to avoidable and hard to find bugs.

Conversion to Integer

VBA has a conversion function CInt() that will try to convert anything into a number of the type Integer.

The function CInt() is not the same as the function Int(). The function Int() returns the largest non-fractional number that is lower or equal to the argument.

Bit operations

VBA supports a limited number of bit operations. Better not to use them.