WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA Integer

VBA Integer

An integer is a data type in VBA which is given to any variable to hold integer values, the limitations or the bracket for the number of an integer variable can hold is similar in VBA to as those of other languages, any variable is defined as integer variable using the DIM statement or keyword in VBA.

Excel VBA Integer

Data types are so important in any coding language because all the variable declaration should be followed by the data type assigning to those variables. We have several data types of working with, and each data type has its own advantages and disadvantages associated with it. When we are declaring variables is important to know details about the particular data type. This is the article dedicated to the “Integer” data type in VBA. We will show you the complete picture of the “Integer” data type.

What is the Integer Data Type?

Integers are whole numbers, which could be positive, negative, and zero but not a fractional number. In the VBA context, “Integer” is a data type we assign to the variables. It is a numerical data type that can hold whole numbers without decimal positions. Integer data type 2 bytes of storage, which is half of the VBA LONG datatype i.e., 4 bytes.

VBA Integer

Examples of Excel VBA Integer Data Type

Below are the examples of the VBA Integer Data type.

You can download this VBA Integer Data Type Template here – VBA Integer Data Type Template

Example #1

When we declare a variable, it is necessary to assign a data type to it and integer one of them, which is commonly used by all the users based on the requirements.

As I said, an integer can only hold of whole numbers, not any fractional numbers. Follow the below steps to see the example of a VBA integer data type.

Step 1: Declare the variable as Integer.

Code:

Sub Integer_Example()

  Dim k As Integer

End Sub

VBA Integer Example 1

Step 2: Assign the value of 500 to the variable “k.”

Code:

Sub Integer_Example1()

  Dim k As Integer

  k = 500

End Sub

VBA Integer Example 1-1

Step 3: Show the value in the VBA message box.

Code:

Sub Integer_Example1()

  Dim k As Integer

  k = 500

  MsgBox k

End Sub

VBA Integer Example 1-2

When we run the code using the F5 key or manually then, we can see 500 in the message box.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

VBA Integer Example 1-3

Example #2

Now I will assign the value as -500 to the variable “k.”

Code:

Sub Integer_Example2()

  Dim k As Integer

  k = -500

  MsgBox k

End Sub

VBA Integer Example 2

Run this code manually or press F5. Then, it will also show the value of -500 in the message box.

VBA Integer Example 2-1

Example #3

As I told VBA, the Integer data type can hold only whole numbers, not fraction numbers like 25.655 or 47.145.

However, I will try to assign the fraction number to a VBA Integer data type. For example, look at the below code.

Code:

Sub Integer_Example3()

    Dim k As Integer

    k = 85.456

    MsgBox k

End Sub

Example 3

I have assigned 85.456 to the variable “k.” I will run this VBA code to see what the result is.

VBA Integer Example 3-1

  • It has returned the result as 85, even though I have assigned the value of the fraction number. This is because of VBA round the fraction numbers to the nearest integer.
  • All the fraction numbers, which are less than 0.5, will be rounded down to the nearest integer. For an example 2.456 = 2, 45.475 = 45.
  • All the fraction numbers, which are greater than 0.5, will be rounded up to the nearest integer. For an example 10.56 = 11, 14.789 = 15.

To have another look at the roundup integer lets the value of “k” to 85.58.

Code:

Sub Integer_Example3()

    Dim k As Integer

    k = 85.58

    MsgBox k

End Sub

Example 3-2

When I run this code using the F5 key or manually, it will return 86 because anything more than 0.5 will be rounded up to the next integer number.

VBA Integer Example 3-3

Limitations of Integer Data Type in Excel VBA

Overflow Error: Integer data type should work fine as long as the assigned value is between -32768 to 32767. The moment it crosses the limit on either side, it will cause you an error.

For example, look at the below code.

Code:

Sub Integer_Example4()

    Dim k As Integer

    k = 40000

    MsgBox k

End Sub

Example 4

I have assigned the value of 40000 to the variable “k.”

Since I have complete knowledge of Integer Data Type for sure, I know it doesn’t work because integer data type cannot hold the value anything more than 32767.

Let’s run the code manually or through the F5 key and see what happens.

VBA Integer Example 4-1

Oops !!!

I got the error as “Overflow” because the Integer data type cannot hold anything more than 32767 for positive numbers and -32768 for negative numbers.

Type Mismatch Error: Integer data can only hold numerical values between -32768 to 32767. Suppose any number assigned more than these numbers will show an Overflow error.

Now I will try to assign text or string values to it.  In the below example code, I have assigned the value as “Hello.”

Code:

Sub Integer_Example4()

    Dim k As Integer

    k = "Hello"

    MsgBox k

End Sub

Example 4-2

I will run this code through the run option or manually and see what happens.

VBA Integer Example 4-3

It is showing the error as “Type mismatch” because we cannot assign a text value to the variable “integer data type.”

Recommended Articles

This has been a guide to VBA Integer Data type in excel. Here we discussed how to use the VBA Integer data type in Excel and its limitations along with the examples and downloadable excel template.

  • RoundUp in VBA
  • 2 Types of Data Types in VBA
  • VBA Function IsEmpty
  • How to Use Paste Special in VBA?
0 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download VBA Integer Data Type Template

New Year Offer - VBA Training Course (6 courses, 35+ hours video) View More