Modulus in VBA: How to Use the Mod Operator

VBA & macros · Updated November 23, 2026

In VBA, the modulus operator is Mod, and it returns the remainder of a division. For example, 10 Mod 3 returns 1 because 10 divided by 3 equals 3 with a remainder of 1. Use it like result = number1 Mod number2 in any VBA code, including Excel macros.

What the Mod Operator Does

The Mod operator in VBA performs integer division and returns the remainder. It works with numeric data types such as Integer, Long, Single, Double, and Currency. The result takes the sign of the divisor (the number on the right). For instance, -7 Mod 3 returns 2, while 7 Mod -3 returns -2. This is different from some other languages where the sign of the result matches the dividend.

Here is a simple example in a VBA subroutine:

Sub ShowRemainder()
    Dim dividend As Long
    Dim divisor As Long
    Dim remainder As Long
    
    dividend = 17
    divisor = 5
    remainder = dividend Mod divisor
    
    MsgBox remainder   ' Displays 2
End Sub

Before running any macro, save a copy of your workbook because macros cannot be undone with Ctrl+Z.

Why Use Mod in VBA?

The modulus operator is useful for several common programming tasks:

  • Checking if a number is even or odd: If number Mod 2 = 0 Then tells you it’s even.
  • Cycling through a fixed set of values: For example, to repeat colors or categories every N rows, use counter Mod N.
  • Extracting digits from a number: For instance, the last digit of a number is number Mod 10.
  • Limiting array indices: To wrap around an array, use index Mod arrayLength.
  • Formatting output: Insert a line break every 10 items in a loop.

These patterns appear in data processing, report generation, and game logic in VBA.

How to Use Mod with Different Data Types

VBA’s Mod accepts floating-point numbers, but the result is always the remainder of the division after rounding the operands to integers? Actually, VBA converts both operands to integers before performing the operation. If either operand is a floating-point number, it is rounded to the nearest integer first. For example, 10.6 Mod 3 is evaluated as 11 Mod 3, giving 2. This can cause unexpected results, so be careful when using non-integer values.

If you need a true floating-point remainder (like the MOD function in Excel), you can use the worksheet function in VBA:

Dim result As Double
result = Application.WorksheetFunction.Mod(10.6, 3)   ' Returns 1.6

This uses Excel’s MOD function, which returns the remainder with decimal precision.

Common Errors and How to Avoid Them

One frequent error is Division by zero. Just like with the / operator, if the divisor is zero, VBA raises a runtime error 11. Always check that the divisor is not zero before using Mod.

Another issue is overflow. If you use Mod with very large numbers that exceed the range of the Integer or Long data type, you may get an overflow error. For example, 30000 Mod 20000 is fine, but 30000 * 30000 Mod 2 would overflow because the intermediate multiplication exceeds Long’s limit. Use the LongLong data type in 64-bit VBA if needed.

Also, remember that Mod has lower precedence than multiplication and division. In an expression like 10 + 3 Mod 2, VBA evaluates 3 Mod 2 first, giving 1, then adds 10, resulting in 11. Use parentheses to make your intention clear.

Mod vs. the Excel MOD Function

Excel’s worksheet function MOD and VBA’s Mod operator differ in how they handle negative numbers. In Excel, MOD(-7, 3) returns 2, and MOD(7, -3) returns -2. In VBA, -7 Mod 3 also returns 2, and 7 Mod -3 returns -2. So they actually match for these cases. However, with floating-point numbers, Excel’s MOD returns a decimal remainder, while VBA’s Mod rounds to integers first. For example, Excel’s MOD(10.6, 3) returns 1.6, but VBA’s 10.6 Mod 3 returns 2. If you need exact decimal remainders, use the worksheet function.

Another difference: the Excel MOD function is available in VBA via Application.WorksheetFunction.Mod, but it may be slower than the native operator because it crosses the boundary between VBA and Excel.

Practical Examples in Excel VBA

Here are a few concrete uses of Mod in real macros:

  1. Color every other row: Loop through rows and use rowNumber Mod 2 to determine if the row is even or odd, then apply formatting.
  2. Split a list into columns: When writing a long list to a worksheet, use counter Mod 10 to decide when to move to the next column.
  3. Check if a year is a leap year: A year is a leap year if it is divisible by 4 but not by 100, unless it is also divisible by 400. You can code that with year Mod 4, year Mod 100, and year Mod 400.
  4. Generate a repeating pattern: For a cycle of 7 days, use dayNumber Mod 7 to map to a weekday name.

Example: Highlight even rows in a selected range.

Sub HighlightEvenRows()
    Dim rng As Range
    Dim cell As Range
    
    Set rng = Selection
    For Each cell In rng.Rows
        If cell.Row Mod 2 = 0 Then
            cell.Interior.Color = RGB(200, 200, 255)
        End If
    Next cell
End Sub

Remember to save your workbook before running this macro.

Performance and Alternatives

The Mod operator is very fast because it is a native VBA operation. For most purposes, performance is not a concern. However, if you are working with extremely large loops (millions of iterations), consider using integer arithmetic with And for powers of two. For example, to check if a number is even, number And 1 is slightly faster than number Mod 2 because bitwise operations are quicker. But the difference is negligible in normal use.

If you need a true modulo operation that always returns a non-negative result (like in mathematics), you can write a custom function:

Function TrueMod(ByVal a As Long, ByVal b As Long) As Long
    TrueMod = ((a Mod b) + b) Mod b
End Function

This ensures the result is always between 0 and b-1, even for negative dividends.

Frequently Asked Questions

Q: What does Mod do in VBA exactly?
A: It returns the remainder after dividing two numbers. For example, 10 Mod 3 returns 1. It’s the same as the mathematical concept of modulus.

Q: Can I use Mod with decimal numbers in VBA?
A: Yes, but VBA rounds the numbers to integers first. If you need a decimal remainder, use Excel’s MOD function via Application.WorksheetFunction.Mod.

Q: Why does my Mod operation give a negative result?
A: In VBA, the result takes the sign of the divisor. For example, -7 Mod 3 gives 2, but 7 Mod -3 gives -2. If you want a non-negative result, use the custom TrueMod function shown above.

Q: Is there a difference between VBA Mod and Excel’s MOD function?
A: Yes, mainly with floating-point numbers. VBA rounds to integers; Excel’s MOD returns a decimal remainder. For negative numbers, they behave the same.

Q: How do I check if a number is even in VBA?
A: Use If number Mod 2 = 0 Then. This works for both positive and negative integers.

Q: Can Mod cause an error in VBA?
A: Yes, if the divisor is zero, you get a runtime error 11. Also, overflow can occur with very large numbers. Always validate your inputs.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.