VBA & Macro Problems

Use the VBA Mod Operator and Decimal Remainders in Excel

Emma BrownEmma Brown Nov 22, 2026 868 views

Question details

Find the VBA equivalent of Excel's MOD function for integer and decimal values.

How to Calculate Remainders with VBA in Excel
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.
Before you start

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.

Solution 1Recommended

Use the VBA Mod operator for integer values

Use the built-in Mod operator when both operands are integers.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic, then choose Insert > Module.

2
Add the integer calculation

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

3
Run the macro

Place the cursor inside IntegerRemainder and select Run > Run Sub/UserForm. The message box displays 2.

Use the VBA Mod operator for integer values
Integer and sign behavior: VBA Mod rounds fractional operands to integers before calculating. For negative values, its result follows the sign of the dividend, which can differ from Excel's MOD function.
Free Microsoft Office alternative

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.

Calculate worksheet remainders with the MOD formula.Open and edit Microsoft Excel XLSX files.Work in a familiar interface that eases the move from Excel.Use a free, lightweight office suite for everyday spreadsheet tasks.
microsoft office alternative - wps office

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.