# How to Quickly Understand Integer in VBA in 3 Minutes (Excel)

Written by Kasper Langmann

Understanding integers in Visual Basic for Applications (VBA) is a crucial aspect of programming in Excel. This article will guide you through the basics of integers in VBA, including their definition, usage, and limitations. We will also delve into some practical examples to help you grasp the concept quickly and efficiently.

## What is an Integer in VBA?

An integer in VBA is a data type that stores whole numbers. Unlike other data types, integers do not accommodate decimal points. They are used when you need to perform calculations or operations that require whole numbers.

In VBA, integers are declared using the keyword ‘Integer’. The range of values an integer can hold in VBA is from -32,768 to 32,767. This range is due to the 16-bit storage size of the integer data type in VBA.

## How to Declare an Integer in VBA

Declaring an integer in VBA is straightforward. You simply use the ‘Dim’ keyword followed by the variable name and the ‘As Integer’ statement. For instance, to declare an integer variable named ‘num’, you would write:

`Dim num As Integer`

This statement creates an integer variable named ‘num’. You can then assign a value to ‘num’ within the range of -32,768 to 32,767. If you try to assign a value outside this range, VBA will return an overflow error.

## Practical Examples of Using Integers in VBA

Let’s delve into some practical examples to further understand how integers work in VBA.

### Example 1: Basic Arithmetic Operations

Integers in VBA can be used to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. Consider the following example:

`Dim num1 As Integer`

`Dim num2 As Integer`

`num1 = 10`

`num2 = 20`

`Debug.Print num1 + num2`

In this example, we declare two integer variables, ‘num1’ and ‘num2’, and assign them the values 10 and 20, respectively. We then print the sum of ‘num1’ and ‘num2’ using the ‘Debug.Print’ statement. The output will be 30.

### Example 2: Using Integers in Loops

Integers are commonly used in loops in VBA. For instance, consider the following example:

`Dim i As Integer`

`For i = 1 To 10`

`Debug.Print i`

`Next i`

In this example, we declare an integer variable ‘i’ and use it as a counter in a ‘For’ loop. The loop prints the numbers 1 through 10.

## Limitations of Integers in VBA

While integers are incredibly useful in VBA, they do have some limitations. The most significant limitation is their range. As mentioned earlier, an integer in VBA can only hold values from -32,768 to 32,767. If you need to work with larger numbers, you will need to use a different data type, such as ‘Long’.

Another limitation of integers is that they cannot hold decimal values. If you need to work with decimal numbers, you should use the ‘Single’ or ‘Double’ data types.

## Conclusion

Understanding integers in VBA is crucial for anyone looking to perform calculations or operations that involve whole numbers in Excel. This guide has provided an overview of integers in VBA, including their definition, how to declare them, practical examples of their use, and their limitations. With this knowledge, you should be able to use integers effectively in your VBA programming.