In VBA, MOD is an operator, not a function and this operator helps you to divide two numbers and returns the remainder value in the result.
It’s equivalent to the mod function in Excel.
You can use the mod in so many ways and, in this tutorial, we will see some examples. Use the below steps to use the mod operator in VBA:
Range("A1") = 10 Mod 3
- Specify the first number which you want to get divided.
- After that, enter the “mod” operator.
- Now, enter a number that you want to divide by.
- In the end, use a message box or a cell to get the remainder from the division.
In the same way, you can also get the remainder using a message box.
And in the following code, we used a message box and then used the mod operator to get the remainder after dividing.
MsgBox 9 Mod 3
And when you run this code, it returns zero in the result in the message box as when you divide 9 by 3 there’s no remainder left, so as in the result of this code.
Note: As I said, there’s also a function in Excel to get the remainder from the division of two numbers and there are a few situations where you will find that the result you get from Excel will be different from the result you got in VBA.
Error in MOD
If you try to divide a number with zero it always returns a division by zero error.
Debug.Print 10 Mod 0
And the above code returns that error.