Mod Operator

Divides two numbers and returns only the remainder.

Syntax

result = operand1 Mod operand2

Remarks

The modulus, or remainder, operator divides operand1 by operand2 (rounding floating-point numbers to integers) and returns only the remainder as result.  For example, in the expression  A = 19 Mod 6.7, A (which is result) equals 5.  The operands can be any numeric expression.

Usually, the data type of result is an Integer, a Long, or a Variant  of VarType 2 (Integer) or VarType 3 (Long).  However, result is a Null (VarType 1) if one or both operands are Null expressions.  Any operand that is Empty (VarType 0) is treated as 0.

Arithmetic Operators

Comparison Operators

Concatenation Operators

Logical Operators

Operator Precedence

Other Operators

VarType Function

Mod Operator Example

The example uses the Mod operator to determine if a 4-digit year is a leap year.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

Sub Form_Click ()

Dim TestYr, LeapStatus                      ' Declare variables.

TestYr = InputBox("Enter a 4-digit year.")

If TestYr Mod 4 = 0 And TestYr Mod 100 = 0 Then   ' Divisible by 4?

If TestYr Mod 400 = 0 Then               ' Divisible by 400?

LeapStatus = " is a leap year."

Else

LeapStatus = " is a centesimal year, but not a leap year."

End If

ElseIf TestYr Mod 4 = 0 Then

LeapStatus = " is a leap year."

Else

LeapStatus = " is not a leap year."

End If

MsgBox TestYr & LeapStatus                  ' Display results.

End Sub