Dividing

VBA is unusual in that it provides two division operators, one for decimal division and one for integer division.
Any time you use the floating-point division operator (/), you're performing floating-point division, and your return value will be of type Double.
This is true whether your dividend and divisor are integer, floating-point, or fixed-point values. It's true whether or not your result has a decimal portion.
For example, running the following code from the Immediate window prints "Double":


Decimal Division Operator - /

The forward slash operator performs decimal division.

Debug.Print 7 / 3           '= 2.33333333333333  
Debug.Print 9 / 4 '= 2.25
Debug.Print 12 / 5 '= 2.4
Debug.Print 20 / 3 '= 6.66666666666667
Debug.Print 100 / 3 '= 33.3333333333333
Debug.Print 10 / Null '= Null
Debug.Print Null / 10 '= Null

This operator divides one number by another and the data type that is returned depends on the types of values.
This is an explicit widening conversion.

Debug.Print TypeName(10 / 3)        'Double  
Debug.Print TypeName(2.34 / 5.9) 'Double
Debug.Print TypeName(9 / 3) 'Double

Integer Division Operator - \

The back slash operator performs integer division.

Debug.Print 7 \ 3           '= 2  
Debug.Print 9 \ 4 '= 2
Debug.Print 20 \ 3 '= 6
Debug.Print 100 \ 3 '= 33
Debug.Print 10 \ Null '= Null
Debug.Print Null \ 10 '= Null

This is a great way of dividing and rounding down in one simple operation.
Before performing the division any floating-point values are converted to long integers.

Debug.Print TypeName(9 \ 3)             'Integer  

The data type returned is a numeric type appropriate for the data types used

Dim myLong As Long 
myLong = 2147486647
Debug.Print TypeName(myLong \ 1) ' "Long"

Mod Operator

You can find the remainder in Integer division by using the modulus operator (mod)
The Mod operator returns the remainder after division.
The MOD function is actually equivalent to the following maths formula

a - (b * (a \ b)) 

This is similar to the integer division except it only returns the remainder.

8 Mod 3 = 2 
10 mod 3 = 1
12.6 Mod 5 = 3

x Mod 2 = 0 'even numbers
x Mod 2 = 1 'odd numbers

You can get overflow errors in VBA using the Mod operator with very large numbers. For example,

Public Sub Mod_Error() 
    Dim Number As Double
    Dim Divisor As Double
    Dim Result As Double
    Number = 2 ^ 31
    Divisor = 7
    Result = Number Mod Divisor ' Overflow error here.
End Sub

Loops

This is extremely useful when you have a loop and you want to perform an action every nth times through a loop.

For icounter = 1 To 100 
   If icounter Mod 10 Then
'every tenth value
   End If
Next icounter

Example

The division operator here is taking an integer value and dividing by a decimal.

Dim n as double 
n = 19.333333
MsgBox CInt( n * 4 ) / 4

The forward slash (/) returns a floating point answer
12 / 5 = 2.4
The answer is returned as a Double.
If you are assigning the result to an Integer, the decimal part will be lost.
This must be explicitely cast if Option Strict is turned on since this is a narrowing cast.


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext