Is there a vba equivalent to excel's mod
function?
9 Answers

- 102,349
- 23
- 137
- 192
-
6+1 the VBA language reference page for `Mod` is [here](http://msdn.microsoft.com/en-us/library/office/gg264708%28v=office.14%29.aspx) – barrowc Jul 25 '13 at 21:39
-
1The mod operator is not equivalent because it cannot return floating precision, unlike the excel function. – johnzilla Sep 21 '16 at 15:35
-
3Be aware that the Excel MOD function calculates differently than the VBA Mod operator when negative numbers are used. I just discovered this with simple integers. I expected to see more explanation of this issue on this page. In VBA `11 Mod -5 = 1` where as Excel's formula `MOD(11,-5) = -4` In my case I decided to use the XLMod() function shown on this page instead of the Mod VBA operator. – Ben Aug 20 '18 at 22:03
You want the mod operator.
The expression a Mod b is equivalent to the following formula:
a - (b * (a \ b))
Edited to add:
There are some special cases you may have to consider, because Excel is using floating point math (and returns a float
), which the VBA function returns an integer. Because of this, using mod
with floating-point numbers may require extra attention:
Excel's results may not correspond exactly with what you would predict; this is covered briefly here (see topmost answer) and at great length here.
As @André points out in the comments, negative numbers may round in the opposite direction from what you expect. The
Fix()
function he suggests is explained here (MSDN).
-
4You'd better change `(a \ b)` for `Fix(a / b)`. Otherwise, you may have problems with decimal arguments. Try your formula with `a = 1.75` and `b = 1` and you'll see my point. – André Jul 25 '13 at 16:01
-
1The formula suggested is wrong and will always produce 0 as answer – PSVSupporter Oct 07 '16 at 08:45
-
2@PSVSupporter I think you're confusing the ` \ ` operator with `/`. They're different. – egrunin Oct 08 '16 at 15:56
-
1
My way to replicate Excel's MOD(a,b)
in VBA is to use XLMod(a,b)
in VBA where you include the function:
Function XLMod(a, b)
' This replicates the Excel MOD function
XLMod = a - b * Int(a / b)
End Function
in your VBA Module

- 20,200
- 11
- 84
- 98

- 109
- 1
- 2
Be very careful with the Excel MOD(a,b) function and the VBA a Mod b operator. Excel returns a floating point result and VBA an integer.
In Excel =Mod(90.123,90) returns 0.123000000000005 instead of 0.123 In VBA 90.123 Mod 90 returns 0
They are certainly not equivalent!
Equivalent are: In Excel: =Round(Mod(90.123,90),3) returning 0.123 and In VBA: ((90.123 * 1000) Mod 90000)/1000 returning also 0.123

- 91
- 1
- 2
-
1There are many warnings about floating point results. +1 for providing an example. – pjvleeuwen Jan 26 '19 at 19:43
The Mod
operator, is roughly equivalent to the MOD
function:
number Mod divisor
is roughly equivalent to MOD(number, divisor)
.

- 263,068
- 57
- 365
- 409
Function Remainder(Dividend As Variant, Divisor As Variant) As Variant
Remainder = Dividend - Divisor * Int(Dividend / Divisor)
End Function
This function always works and is the exact copy of the Excel function.

- 775
- 5
- 15
-
While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Erty Seidohl May 17 '18 at 21:28
I've found this to be the most reliable if excel's mod function is also giving incorrect results:
Originally even XLMod = (a - (b * Int(a / b))) was giving me incorrect results so..
Function XLMod(a, b As Long)
' This replicates the Excel MOD function - the VBA mod function returns an integer
Dim templong As Long
templong = (b * Int(a / b))
XLMod = (a - templong)
End Function

- 300
- 4
- 14
-
1Var type has to be declared for each variable. Parameter `a` will be a variant in this case. Double might also be preferable. – Filcuk Mar 11 '22 at 09:44
The top answer is actually wrong.
The suggested equation:
a - (b * (a \ b))
Will solve to: a - a
Which is of course 0 in all cases.
The correct equation is:
a - (b * INT(a \ b))
Or, if the number (a) can be negative, use this:
a - (b * FIX(a \ b))

- 61
- 1
- 2
- 6
-
4\ operator is not the same as / operator \ The result is the integer quotient of expression1 divided by expression2, which discards any remainder and retains only the integer portion. This is known as truncation. The / Operator (Visual Basic) returns the full quotient, which retains the remainder in the fractional portion. https://msdn.microsoft.com/en-us/library/0e16fywh.aspx – Cheeky Charlie Jul 18 '16 at 11:42
-
-1 As explained by Cheeky Charlie this answer is misleading, based on an incorrect assumption; if you aren't convinced, try entering _Msgbox 3 - (2 * (3 \ 2))_ in the Immediate window in the VBA editor, which returns 1 as expected from the modulo function instead of 0 as posited by this poster. – pbeentje Feb 22 '19 at 14:36