Thursday, March 3, 2011

No Function MOD(a,b) in VBA

Can you believe that in VBA you cannot call MOD(a,b) function although it works fine in Excel cells??!
Well, you can use the operator a MOD b if both a and b are integer numbers.
But there is no direct MOD function in VBA because VBA 2003 does not support MOD (number, dividor) as a WorksheetFunction.
Therefore, I have to write the following function:

Function fmod(a As Double, b As Double)
fmod = a - b * (a \ b)
End Function

1 comment:

  1. MOD is an operator in VBA just like \, +, -, or *. It is not a function.

    The syntax is:
    a MOD b

    ReplyDelete