Está en la página 1de 6

Excel MOD function to get remainder and more

ablebits.com /office-addins-blog/2017/09/13/excel-mod-function-remainder/

Excel MOD function - syntax and basic uses


The MOD function in Excel is used to find remainder after division of one number (dividend) by another (divisor).
In mathematics, this operation is called the modulo operation or modulus, hence the name MOD.

The MOD function has the following syntax:

MOD(number, divisor)

Where:

Number (required) - the number to be divided.


Divisor (required) - the number to divide by.

For example, MOD(10,3) returns 1 because 10 divided by 3 has a quotient of 3 and leaves a remainder of 1
(10=3*3+1). The formula MOD(10,5) returns zero because 10 is divided by 5 without remainder.

3 things you should know about MOD in Excel

1. The result of the MOD function has the same sign as the divisor.
2. If the divisor is 0, MOD returns the #DIV/0! error because you cannot divide
by zero.
3. If the number or divisor is a text value, an Excel Mod formula returns the
#VALUE! error.

MOD formulas to calculate cells


In real-life worksheets, the Excel MOD function is rarely used on its own. More often, you will find it as part of
bigger formulas that perform various calculations based on division remainder.

Mod formula to sum every Nth row or column

Surprising at it may seem, Microsoft Excel has no built-in function to sum every 2nd, 3rd, etc. row in a worksheet.
However, that can be easily done by using one of the following formulas.

Sum every other row

To add up values in every second row, use MOD in conjunction with ROW and SUMPRODUCT:

Sum even rows:

SUMPRODUCT((MOD(ROW(range),2)=0)*(range))

Sum odd rows:

SUMPRODUCT((MOD(ROW(range),2)=1)*(range))

Assuming B2:B7 are the cells to sum, the formulas look as follows:

Sum even rows:

1/6
=SUMPRODUCT((MOD(ROW($B$2:$B$7),2)=0)*($B$2:$B$7))

Sum odd rows:


=SUMPRODUCT((MOD(ROW($B$2:$B$7),2)=1)*($B$2:$B$7))

The screenshot below shows the result:

In these formulas, the MOD ROW combination determines which rows to sum and SUMPRODUCT adds up the
values. Here's how:

ROW supplies an array of row numbers to the number argument of the MOD function. MOD divides each row
number by 2, and you check the result:

To sum even rows, the result should be 0 because even numbers are divisible by 2 evenly, without
remainder.
To sum odd rows, the result should be 1 because odd numbers divided by 2 leave a remainder of 1.

Instead of SUMPRODUCT, you could use the IF function to evaluate the condition and SUM to add up the
numbers (it's an array formula, which is entered by pressing Ctrl + Shift + Enter):

=SUM(IF(MOD(ROW($B$2:$B$7),2)=0,$B$2:$B$7,0))

Personally, I'd stick with SUMPRODUCT because ROW is a volatile function and using it in an array formula
makes the whole formula volatile. That means the formula would recalculate with every change you make to a
workbook, which may noticeably slow down your Excel.

Sum every Nth row

To add up every 3rd, 4 th, 5 th, etc. row, use this generic formula:

SUMPRODUCT((MOD(ROW(range)-ROW(first_cell)+1,n)=0)*(range))

For example, to sum every 3 rd cell in the range C2:C10, the formula goes as follows:

=SUMPRODUCT((MOD(ROW($C$2:$C$10)-ROW($C$2)+1,3)=0)*($C$2:$C$10))

Compared to the previous example, this formula has a bit more complex "filtering logic":

MOD(ROW($C$2:$C$10)-ROW($C$2)+1,3)=0

First, you subtract the first row from all other rows in the range to get an array of relative row numbers
{0;1;2;3;4;5;6;7;8}, to which you add 1 to start counting from one: {1;2;3;4;5;6;7;8;9}

The above array goes into the number argument of the MOD function, which returns the remainder after dividing
each number by 3: {1;2;0;1;2;0;1;2;0}
2/6
Note that zeros appear for every 3 rd row, and MOD()=0 tells Excel to return TRUE only for these rows, FALSE for
all other rows. With TRUE equating to 1 and FALSE to 0, we are left with an array of {0,0,1,0,0,1,0,0,1}

Finally, SUMPRODUCT multiplies the above array and C2:C10, and then adds up the products. Since
multiplying by zero always gives 0, only the rows that contain 1 in the first array survive the multiplication.

Sum every Nth column

To sum numbers in every 2nd, 3 rd, etc. column, simply replace ROW with COLUMN in the above formula, like
this:

SUMPRODUCT((MOD(COLUMN(range)-COLUMN(first_cell)+1,n)=0)*(range))

For example, to add up numbers in every 4 th column in the range B2:I2, you go with this formula:

=SUMPRODUCT((MOD(COLUMN($B2:$I2)-COLUMN($B2)+1,4)=0)*($B2:$I2))

Mod formula to concatenate every N cells

In situations when the details relating to the same item are split into several cells, combining the contents of
every N number of cells into one cell may be a challenge.

In this example, we have order details in three different cells in column B, so we are looking to concatenate each
block of three cells, beginning with B2. This can be done with the following formula:

=IF(MOD(ROW()-1, 3)=0, CONCATENATE(B2, "-", B3, "-", B4),


"")

Important note! For the formula to work correctly, it shall be entered in the 3 rd row with data (C4 in this
example), and then copied down the column.

The formula's logic is very simple:

First, you check if a given row is divisible by 3 with no remainder: MOD(ROW()-1, 3)=0. To get the relative
position of the current row, we subtract 1 from the actual row number (since our data begins in row 2, not in row
1). If the division remainder is equal to 0, the CONCATENATE function combines values from 3 cells and
separates them with the delimiter of your choosing ("-" in this example). The use of relative cell references
ensures that three different cells are concatenated each time.

Tip. Instead of the -1 correction, you can get the relative position of each row as we did in the above example,
i.e. IF(MOD(ROW()-ROW($B$2)+1, 3)=0, CONCATENATE(), "")

This will make your formula more flexible and it won't require further adjustments no matter in which row your
data starts.
3/6
Mod formulas to count cells that contain odd or even numbers

As you already know, the easiest way to identify an even integer is divide the number by 2 and check for zero
remainder. To identify an odd number, you check for a remainder of 1. Again, we will be using the MOD function
to find remainders, and SUMPRODUCT to count the "filtered cells".

Count cells with odd numbers:

SUMPRODUCT((MOD(range,2)=1)*1)

Count cells with even numbers:

SUMPRODUCT((MOD(range,2)=0)*1)

With numbers in cells A2 to A8, the formulas take the following shape:

Count cells containing odd numbers:

=SUMPRODUCT((MOD(A2:A8,2)=1)*1)

Count cells containing even numbers:

=SUMPRODUCT((MOD(A2:A8,2)=0)*1)

How to use MOD in Excel to highlight cells


Aside from calculating cells based on a specific remainder, the MOD function in Excel can be used to highlight
those cells. For this, you will have to create a formula-based conditional formatting rule. The detailed steps to
set up a rule can be found here, and the formulas are provided below.

Highlight odd and even numbers

To highlight cells containing odd or even numbers, use the following Excel Mod formula that divides the number
by 2, and then checks for a remainder of 1 and 0, respectively:

Highlight cells with odd numbers:

=MOD(A2,2)=1

Highlight cells with even numbers:

=MOD(A2,2)=0

Where A2 is the leftmost cell with data.

4/6
Highlight integers and decimals

To identify integers, just follow this logical chain: any number divided by 1 equals itself, meaning an integer
divided by 1 is always the same integer, meaning the remainder is zero, meaning we can use this simple MOD
formula:

=MOD(A2,1)=0

If the remainder is greater than zero, you are dealing with a fraction (including decimals):

=MOD(A2,1)>0

Since in formula-based conditional formatting rules cell references are relative to the top left cell of the selected
range, we reference A2 in this example:

Highlight multiples of a number

To highlight multiples of a specific value, simply divide a number by that value and check for zero remainder.

For example, to highlight cells containing 10 and all its multiples, use this formula:

=MOD(A3,10)=0

Where A3 is the top left cell in the range for which you create the rule (A3:C9 in the screenshot below).

Or, you can input the number of interest in any empty cell, say C1, and reference that cell in your formula
(remember to lock the cell with an absolute reference):

=MOD(A3,
$C$1)=0

Excel MOD with Data Validation


Another clever use of the Excel MOD function can be preventing entry of certain data in selected cells, such as
odd numbers, even numbers, or decimals. This can be done with the same MOD formulas that we've used to
highlight cells, but this time we will be defining a Data Validation rule.

To make a data validation rule based on a formula, click Data > Data Validation. On the Settings tab, select
Custom in the Allow box, and enter your formula in the Formula box. For the detailed steps, please see How to
create a custom validation rule in Excel.

For example, to allow only integers in cells A2:A8, select these cells, and configure a rule with this formula:
5/6
=MOD(A2,1)=0

In a similar fashion, you can limit the data entry to even or odd numbers:

Allow only odd numbers:

=MOD(A2,2)=1

Allow only even numbers:

=MOD(A2,2)=0

Where A2 is the left-top-most cell of the selected range.

That's how you use the MOD function in Excel to find the division remainder and perform other calculations. To
have a closer look at the formulas discussed in this tutorial, you are welcome to download our sample Excel
MOD workbook. For more formula examples, please check out the resources at the end of this page.

I thank you for reading and hope to see you on our blog next week!

More Excel MOD formula examples:

6/6

También podría gustarte