Use the VBA Mod Operator and Decimal Remainders in Excel
Question details
Find the VBA equivalent of Excel's MOD function for integer and decimal values.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A user wants to calculate a remainder in an Excel VBA macro without relying on WorksheetFunction.Mod.
- Observed behavior
- The desired calculation uses VBA's built-in Mod operator for integers and an explicit formula for decimal values.
Identify whether your inputs are integers or decimals, and make sure the divisor is not zero. Open the workbook in a version of Excel that supports VBA macros.
Use the VBA Mod operator for integer values
Use the built-in Mod operator when both operands are integers.
In Excel, open the Developer tab and select Visual Basic, then choose Insert > Module.
Enter this macro in the module: Sub IntegerRemainder() Dim a As Long, b As Long, result As Long a = 12 b = 5 result = a Mod b MsgBox result End Sub
Place the cursor inside IntegerRemainder and select Run > Run Sub/UserForm. The message box displays 2.

Calculate a decimal remainder with an explicit formula
Use Double variables and an Int-based formula when the inputs contain decimals or you need Excel MOD's sign convention.
Calculate spreadsheet remainders with WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and use spreadsheet formulas such as MOD; check macro compatibility separately when migrating VBA workbooks.

Frequently Asked Questions
Can I use VBA Mod with decimal numbers?
VBA Mod rounds fractional operands to integers first. For a decimal remainder, use Double variables and calculate a - (VBA.Int(a / b) * b), with a nonzero divisor.
Why does VBA Mod give a different result from Excel MOD for negative numbers?
VBA Mod gives a nonzero remainder the sign of the dividend, while Excel MOD uses the sign of the divisor. Use a - (VBA.Int(a / b) * b) to follow Excel's convention.
What happens if the divisor is zero?
A zero divisor causes an error. Check the divisor before calculating, for example with If b = 0 Then, and exit the macro or prompt the user.
How can I calculate a remainder without a macro?
Enter a worksheet formula such as =MOD(12,4.3) in a cell. It returns approximately 3.4.




