totn Excel Functions

MS Excel: How to use the MOD Function (VBA)

This Excel tutorial explains how to use the Excel MOD function with syntax and examples.

Description

The Microsoft Excel MOD function returns the remainder after a number is divided by a divisor.

The MOD function is a built-in function in Excel that is categorized as a Math/Trig Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

Please read our MOD function (WS) page if you are looking for the worksheet version of the MOD function as it has a very different syntax.

Syntax

The syntax for the MOD function in Microsoft Excel is:

number Mod divisor

Parameters or Arguments

number
A numeric value whose remainder you wish to find.
divisor
The number used to divide into the number parameter.

Returns

The MOD function returns a numeric value.
If the divisor is 0, then the MOD function will return return a Run-time error '11': Division by zero error.

Applies To

  • Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The MOD function can be used in VBA code in Microsoft Excel. Here are some examples of what the MOD function would return:

19 Mod 17
Result: 2

19 Mod -13
Result: 6

34 Mod 17
Result: 0

34 Mod 0
Result: Run-time error '11': Division by zero

For example:

Dim LNumber As Integer

LNumber = 19 Mod 17

In this example, the variable called LNumber would now contain the value of 2.