Mod Operator

See Also3NAHGDE              Example103G6P2>Low

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 expression71RISN.

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


See Also

Arithmetic OperatorsSH0K1B

Comparison OperatorsNRJZ1K

Concatenation Operators2MYTW1K

Logical Operators1CQVUTN

Operator Precedence23082A9

Other Operators2HA580P

VarType FunctionXRZH1Y


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