# VBA Integer

## What is Excel VBA Integer?

Excel VBA Integer is a data type used to store whole numbers. It represents 16-bit signed integers, which means it can hold values ranging from -32,768 to 32,767. The VBA Integer data type is typically used when the required values range is within this range, and memory efficiency is a priority. Let us look at an example to calculate the Sum of Even Numbers using a VBA Integer.

In this example, we have an Excel VBA macro that calculates the sum of even numbers from 1 to a specified upper limit.

We declare three VBA Integer variables: “limit,” “sum,” and “i.” The “limit” variable holds the upper limit of the numbers we want to consider for the sum calculation.

The macro uses a FOR loop to iterate through each number from 1 to the specified limit. We use the MOD operator inside the loop to check if the current number is even. The Mod operator calculates the remainder of the division, and if the remainder is 0, then the number is even. If the number is even, we add it to the “sum” variable.

After the loop completes, the macro displays a message box with the sum of even numbers from 1 to the specified limit.

###### Key Takeaways
1. Excel VBA Integer data type stores whole numbers within the range of -32,768 to 32,767.
2. Integer variables consume less memory than other numeric data types but should be used when values fall within the VBA Integer range to avoid overflow errors.
3. Converting between VBA Integer and other data types can be achieved using functions like CInt (Variant to Integer) and CStr (Integer to String).
4. Be cautious about the limitations of the VBA Integer data type and consider using Long for a broader range of values in VBA.

### What is the Integer Data Type in VBA?

The VBA Integer data type is fundamental in programming languages, including VBA. It is used to store whole numbers that do not have fractional parts. In VBA, an Integer variable occupies 2 bytes of memory and can store values from -32,768 to 32,767.

VBA Integer variables are helpful when memory efficiency is critical, as they consume less than other numeric data types like Long, which uses 4 bytes. However, one should be cautious not to use VBA Integer for values that may exceed its range, which can lead to overflow errors.

## Excel VBA – All in One Courses Bundle (35+ Hours of Video Tutorials)

If you want to learn Excel and VBA professionally, then ​Excel VBA All in One Courses Bundle​ (35+ hours) is the perfect solution. Whether you’re a beginner or an experienced user, this bundle covers it all – from Basic Excel to Advanced Excel, Macros, Power Query, and VBA.

### Examples

Let us look at simple examples of using the VBA Integer function.

#### Example #1 – Simple Integer Variable

In this example, we will use Excel VBA to calculate the sum of even numbers from 1 to a specified upper limit.

The macro efficiently identifies even numbers using Integer data type and a FOR loop in VBA. The result will be displayed in a message box, providing the sum of even numbers within the specified VBA Integer range.

Step 1: Open your Excel workbook and access the VBA editor by pressing “Alt + F11.”

Step 2: In the VBE, click “Insert” in the menu bar and select “Module.” It will insert a new module into the project.

Step 3: In the new module, first start by creating a subroutine named “ExampleIntegerVariable.”

Step 4: In this step, we declare a variable named “myNumber” using the Dim statement and specify that it will be of the Integer data type used to store whole numbers.

Step 5: Here, we set the value of the “myNumber” variable to 100.

Step 6: Next, we display the value of “myNumber” in a message box.

This line uses the MsgBox function to display a message box with the text “The value of myNumber is: ” concatenated with the value stored in the “myNumber” variable. The & symbol is used for concatenation in VBA.

Step 7: Now save the module and exit the VBE. Now press Alt + F8 to open the Macro menu, select “ExampleIntegerVariable,” and click Run.

Step 8: Once you execute the code, you will see a message box.

Here is the complete code:

Sub ExampleIntegerVariable()
Dim myNumber As Integer
myNumber = 100
MsgBox “The value of myNumber is: ” & myNumber
End Sub

#### Example #2 – Looping with Integer

In this VBA Integer example, we use a For loop and an Integer variable “i” to iterate from 1 to 5. The macro prints each iteration number to the Immediate Window using “Debug.Print”.

Step 1: In the new module, start by creating a subroutine named “ExampleLoopWithInteger.”

Step 2: In this step, we declare a variable named “i” using the Dim statement and specify that it will be of the Integer data type. This variable will act as a loop counter.

Step 3: Here, start a loop that iterates from 1 to 5 using the For loop and the To keyword.

The For loop is initiated here, with “i” starting from 1 and running through each iteration until it reaches 5. The To keyword defines the range of iterations for the loop.

Step 4: Next, we print the current iteration number to the Immediate Window using Debug.Print

Within the loop, we use the Debug.Print statement to print the text “Iteration: ” concatenated with the current value of “i” to the Immediate Window. The & symbol is used for concatenation in VBA.

Step 5: Repeat the loop until the value of “i” reaches 5.

The Next i statement indicates the end of the loop. It causes the loop to repeat until the value of “i” reaches 5, at which point it terminates.

Step 6: Now save the module and exit the VBE. Press Alt + F8 to open the Macro menu and select “ExampleLoopWithInteger,” and click Run.

Step 7: Once you execute the code, you will the iterations in the immediate window.

Here is the complete code:

Sub ExampleLoopWithInteger()
Dim i As Integer
For i = 1 To 5
Debug.Print “Iteration: ” & i
Next i
End Sub

#### Example #3 – Converting Variant to Integer

This VBA Integer example will demonstrate the conversion between data types using Variant and Integer. Here, we assign the value 50 to a Variant variable “myVariant” and then convert it to an Integer using the Cint function.

Step 1: In the new module, start by creating a new subroutine named “ExampleVariantToInteger.”

Step 2: We declare a variable named “myVariant” using the Dim statement and specify that it will be of the Variant data type. The Variant data type can store values of different data types.

Step 3: In this step, we declare another variable named “myInteger” using the Dim statement and specify that it will be of the Integer data type. This variable will store the converted value.

Step 4: Next, we assign the value 50 to the “myVariant” variable using the assignment operator “=”.

Step 5: Use the CInt function to convert “myVariant” (Variant) to “myInteger” (Integer).

This line uses the CInt function to convert the value stored in “myVariant” (a Variant) to the Integer data type. The converted value is then assigned to the “myInteger” variable.

Step 6: Finally, we use the MsgBox function to display a message box with the text “The value of myInteger is: ” concatenated with the value stored in the “myInteger” variable. The & symbol is used for concatenation in VBA.

Step 7: Now save the module and exit the VBE. Press Alt + F8 to open the Macro menu, select “ExampleVariantToInteger,” and click Run.

Step 8: Once you execute the macro, you will see a message box with the statement “The value of myinteger is: 50”

Here is the complete code:

Sub ExampleVariantToInteger()
Dim myVariant As Variant
myVariant = 50
Dim myInteger As Integer
myInteger = CInt(myVariant)
MsgBox “The value of myInteger is: ” & myInteger
End Sub

### Limitations of Integer Data Type in Excel VBA

The VBA Integer data type has some limitations to be aware of:

• The VBA Integer range of values stored in an Integer variable is from -32,768 to 32,767. Values outside this range will cause overflow errors.
• When performing calculations with VBA Integers, be cautious about possible overflow errors, especially when working with large numbers.

### Important Things To Note

1. While the Integer data type saves memory, be mindful of the range limitations. If you expect larger values or decimals, consider using other data types like Long or Double.
2. The VBA Integer limit is defined by its range. The VBA Integer Max value is 32,767 and the Min value -32,768.
3. To display an Integer value in a message box, you can concatenate it with a String using MsgBox. For example, MsgBox “The value is: ” & “myinteger.” If there is a possibility that the values may exceed the range of Integer, consider using Long, which can handle a wider range of values.

1. How to convert an Integer to a String in VBA?

To convert a VBA Integer to String, use the CStr function. For example, CStr(myInteger) converts an Integer variable “myInteger” to a String.

2. How long can an Integer be in VBA?

In VBA, an Integer data type can hold values within the range of -32,768 to 32,767. Any value outside this range will result in overflow errors.

3. How to convert a Variant to an Integer in VBA?

To convert a Variant to a VBA Integer, use the CInt function. For example, myInteger = CInt(myVariant) converts the Variant variable “myVariant” to an Integer variable “myInteger.”

4. How to concatenate a String and an Integer in Excel VBA?

To concatenate a String and a VBA Integer in Excel VBA, use the & operator. For example, MsgBox “The value is: ” & myInteger will display a message box with the concatenated String and Integer value.

Guide to VBA Integer. Here we learn to use Integer data type to store whole numbers along with examples & downloadable excel template. You can learn more from the following articles –