VBA INT

Last Updated :

-

Edited by :

Reviewed by :

Table Of Contents

arrow

Excel VBA INT (Integer) Function

VBA INT is an inbuilt function that only gives us the integer part of a number that provides input. One may use this function in those data whose decimal part is not so much affecting the data. So to ease the calculations or analysis, we use the INT function to get only the integer values.

This function is available with the worksheet as well as in VBA. The Integer function rounds a specific number down to the nearest integer number.

VBA INT

One can use the VBA Int function in the Sub and Function procedures. Now, look at the syntax of the INT function in VBA.

VBA INT Formula

It has only one argument to supply, Number.

  • The number
  • is the number we are trying to convert to an INTEGER number. When we supply the number, the INT function rounds it down to the nearest Integer.

For example, if the supplied number is 68.54, the Excel VBA INT function rounds down to 68 toward zero. On the other hand, if the supplied number is -68.54, then the INT function rounded the number to 69, away from zero.

How to use VBA INT Function?

Example #1

Look at the simple example of the INT function in VBA. Let us perform the task of rounding the number 84.55 to the nearest integer number.

Step 1: Start the subprocedure.

Step 2: Declare the variable as Integer.

Code:

Sub INT_Example1()

  Dim K As Integer

End Sub
excel vba integer Example 1

Step 3: Now, assign the formula to the variable “k” by applying the INT function.

Code:

Sub INT_Example1()

  Dim K As Integer

  K = Int(

End Sub
excel vba integer Example 1-1

Step 4: Supply the number as 84.55.

Note: Since it is a numerical number, we need not supply double quotes.

Code:

Sub INT_Example1()

  Dim K As Integer

  K = Int(84.55)

End Sub
VBA INT Example 1-2

Step 5: Now pass the variable in the VBA message box.

Code:

Sub INT_Example1()

  Dim K As Integer

  K = Int(84.55)
  MsgBox K

End Sub
excel vba integer Example 1-3

Let us run the code and see what happens.

excel vba integer Example 1-4

So, the result is 84 and rounded down to the nearest integer value.

Example #2

Now with the same number with the negative sign, we will try to convert to the nearest integer value. Below is the code with a negative sign.

Code:

Sub INT_Example2()

  Dim k As Integer

  k = Int(-84.55)
    'Negative number with INT function
  MsgBox k

End Sub

When we run this code, it has rounded the number to -85.

VBA INT Example 2

Example #3 - Extract Date Portion from Date & Time

A few times in our careers, we have encountered the scenario of extracting the date and time separately from the combination of Date and Time.

For example, look at the below data of Date and Time.

Extract Date & Time Example 3

How do you extract the Date and Time portion?

First, we need to extract the Date portion. For this, let us apply the INT function. The below code will extract the DATE portion from the above data.

Code:

Sub INT_Example2()

  Dim k As Integer

  For k = 2 To 12
    Cells(k, 2).Value = Int(Cells(k, 1).Value)
    'This will extract date to the second column

    Cells(k, 3).Value = Cells(k, 1).Value - Cells(k, 2).Value
    'This will extract time to the third column
  Next k

End Sub

Run this code. You might get the result like the below (if the formatting is not applied).

Extract Date & Time Example 3-1

In the date column, apply the formatting as "DD-MMM-YYYY," and for the time column, apply the date format in excel as "HH:MM: SS AM/PM."

Date & Time column