Showing posts with label Mod. Show all posts
Showing posts with label Mod. Show all posts

Sunday, October 2, 2022

Mod Operator in Excel VBA

Operator Mod is Used to divide two numbers and return only the remainder after round.

  • If number 10/2 = 5 with no balance, so 10 Mod 2 = 0.
  • If number 10/3 =  3.33 is equivalent to 9/3 =3 with balance 1, so 10 Mod 3 = 1

Please refer below example for more detail.

VBA Code:

Option Explicit
Sub ComparisonOperator_Mod()

    Dim i As Integer
    With ActiveSheet
        i = 2
        Do
            'Mod operator
            If IsNumeric(.Range("B" & i)) And _
            IsNumeric(.Range("C" & i)) Then
                .Range("D" & i) = .Range("B" & i) Mod .Range("C" & i)
            End If
            i = i + 1
        Loop While .Range("A" & i) <> ""
    End With
    
End Sub

Microsoft Reference-Mod-operator
Microsoft Reference-Mod-operator example

Practice makes perfect. Thank You.