# How to use the MOD function

**What is the MOD function?**

The Mod function returns the remainder after a number is divided by a divisor. The Mod function is short for the Modulo operation (wikipedia).

The image above shows that the remainder of 11/2 is 1. 5*2 = 10. 11-10 = 1.

### What's on this page

## 1. Introduction

**What is the Modulo operation?**

The modulo operation gives the remainder. Example, for an equation like 10 modulo 3:

10 is divided by 3 as much as possible, 3 goes into 10 3 times (divisor is 3). The remainder left over is 1, so 10 modulo 3 = 1

**What is a remainder?**

The remainder is what is left over after division of one number by another.

12 /5 = 2 and the remainder is 2. 2*5 = 10 + 2 = 12

**What is a division?**

Division is one of the four basic operations of arithmetic. It refers to the operation of determining how many times one number (the divisor) is contained within another number (the dividend).

It splits a quantity into equal groups or subsets. The result is called the quotient. Any amount left over is the remainder.

**What is a divisor?**

A divisor is a number that divides into another number either cleanly or leaving a remainder.

dividend / divisor = quotient( + remainder)

## 2. Syntax

MOD(*number*, *divisor*)

number |
Required. The number for which you want to find the remainder. |

divisor |
Required. Divisor is number by which you want to divide the number (first argument). |

## 3. Example 1

The image above demonstrates the result the MOD function returns using different numbers in both arguments *number* and *divisor*. The MOD function is in column "Remainder".

- The first number is 34 and the divisor is 1. 34/1 equals 34 with 0 (zero) remainders.
- The second number is 78, the divisor is 4. 78/4 = 19.5 There remainder is 2. 4*19 = 76. 78 - 76 = 2
- The third number is 85, the divisor is 6. 85/6 equals approx. 14.16667. The remainder is 1. 14*6 = 84. 85-84 = 1
- The fourth number is 99 and the divisor is 4. 99/4 = 24.75. The remainder is 3. 24*4 = 96. 99 - 96 = 3
- The fifth number is 62 and the divisor is 5. 62/5 = 12.4. 12*5 = 60 62-60 = 2 The remainder is 2.

## 4. Example 2

This example demonstrates a formula that joins the three last cell values every third row. The following formula concatenates cells in column A every third row:

Lets begin with ROW() in cell B3.Â ItÂ is dynamic and changes forÂ each row. It returns the current row number, example in cell B3 ROW() returns 3.

Then the MOD function takes 3 and divides it with 3. MOD(ROW(), 3) returns 0. Â The remainder is zero in cell B3. You can see this part of the formula in column C and the result in column D.

MOD(ROW(),3)=0 is a logical expression, it checks if the result from the MOD function is equal to 0 (zero). In every third row it is equal and returns TRUE, MOD(ROW(),3)=0 returns TRUE. You can see this part of the formula in column E and the result in column F.

The IF function returns CONCATENATE(A1, A2, A3) if the logical expression is TRUE or a blank if the logical expression is FALSE.

There are relative cell references in the CONCATENATE function and they change in each cell. Don't know much about relative and absolute cell references? Read this: Absolute and relative references in excel

## 5. Example 3

This example demonstrates how to highlight every n-th row using the MOD function. You can use the same technique to highlight every second row with conditional formatting.

Here is how to apply conditional formatting to a cell range:

- Select a cell range.
- Go to the "Home" tab on the ribbon.
- Press with left mouse button on the Conditional formatting button.
- Press with left mouse button on "New Rule...".
- Press with left mouse button on "Use a formula to determine which cells to format:".

- TypeÂ theÂ formula below. Press with left mouse button on the "Format" button. Go to tab "Fill". Pick a color. Press with left mouse button on OK twice.

Conditional formatting formula:

If you want to highlight every third row change the formula toÂ =MOD(ROW(A1),**3**)=0.

Tip! Use Excel Tables to automatically format every other row if you don't want to use formulas and Conditional Formatting. You can easily change the formatting, select any cell in the Excel Table. A new tab appears on the ribbon named "Table Desing".Press with mouse on tab "Table Design". Press with mouse on any table style to quickly change formatting.

## 6. Example 4

This conditional formatting rule highlights every other group based on values in column B. Column B must be sorted.

Conditional formatting formula:

### Explaining CF formula in cell B3

I recommend that you enter the formula in cell F3 to be able to evaluate the formula using the "Evaluate Formula" tool. The formula returns either TRUE or FALSE and the row is highlighted if it returns TRUE.

To start the "Evaluate Formula" tool go to tab "Formulas" on the ribbon.Â Press with left mouse button on the "Evaluate Formula" button and a dialog box shows up.

#### Step 1 - Count cells based on a condition or criteria

The COUNTIF function calculates the number of cells that is equal to a condition.

COUNTIF(*range*,Â *criteria*)

Note that the cell references expand when the cell is copied to the cells below. In this case, the formula is used apply "Conditional Formatting" and the cell references changes depending on which cell is evaluated.

COUNTIF($B$2:$B2,$B$2:$B2)

becomes

COUNTIF("A1","A1")

and returns 1.

#### Step 2 - Divide 1 with the result

1/COUNTIF($B$2:$B2,$B$2:$B2)

becomes

1/1

and returns 1.

#### Step 3 - Sum array

The SUM function adds the numbers in a cell range or array and returns a total.

SUM(1/COUNTIF($B$2:$B2,$B$2:$B2))

becomes

SUM(1)

and returns 1.

#### Step 4 - Divide total with 2 and calculate remainder

MOD(SUM(1/COUNTIF($B$2:$B2,$B$2:$B2)),2)

becomes

MOD(1, 2)

and returns 1. 1 is equal to boolean value TRUE, cell B3 is highlighted.

## 7. Example 5

This example demonstrates how to highlight groups based on values in all three columns: B, C and D. These values are evaluated per row meaning values in column B, C, and D forms a group based on each row.

Conditional formatting formula:

Make sure you get the relative and absolute cell references right.

### Explaining CF formula

#### Step 1 - Count equal rows

The COUNTIFS function calculates the number of cells across multiple ranges that equals all given conditions.

The cell references $B$2:$B2 and so on expands automatically when the CF applies the conditional formatting formula to cells below. This makes the formula remember previous rows and is able to highlight correct rows.

COUNTIFS(*criteria_range1*,Â *criteria1*, [*criteria_range2*,Â *criteria2*]â€¦)

COUNTIFS($B$2:$B2, $B$2:$B2, $C$2:$C2, $C$2:$C2, $D$2:$D2, $D$2:$D2)

becomes

COUNTIFS(1, 1, "A1", "A1", "AA", "AA")

and returns 1.

#### Step 2 - Divide 1 with result

1/COUNTIFS($B$2:$B2, $B$2:$B2, $C$2:$C2, $C$2:$C2, $D$2:$D2, $D$2:$D2)

becomes

1/1

and returns 1.

#### Step 3 - Sum numbers

The SUM function adds the numbers in a cell range or array and returns a total.

SUM(1/COUNTIFS($B$2:$B2, $B$2:$B2, $C$2:$C2, $C$2:$C2, $D$2:$D2, $D$2:$D2)

becomes

SUM(1)

and returns 1.

#### Step 4 - Calculate remainder

MOD(SUM(1/COUNTIFS($B$2:$B2, $B$2:$B2, $C$2:$C2, $C$2:$C2, $D$2:$D2, $D$2:$D2), 2)

becomes

MOD(1,2)

and returns 1.

## 8. Example 6

* How to return the fractional part of a number?*If you use 1 as a divisor the MOD function returns only the fraction of a number, in other words, only the decimals.

*number*Â -Â INT(

*number*/

*divisor*)

You can use this technique to return the time part from a cell containing date and time, see row 3 in the picture above.

## 9. Example 7

* How to calculate the integer portion of a division?*The QUOTIENT function returns a number without the fractional component. 78 / 4 = 19.5

The quotient is 19. Here are more examples, the quotients areÂ in column C.

QUOTIENT(numerator, denominator) uses the same arguments as the MOD function.

## 10. Example 8

* How to remove whole number and return decimal?*The formula in cell C3 removes the whole number and returns only the decimal part.

Formula in cell C3:

The formula above won't work with negative numbers, see cell C6 above. This formula in cell C3 works with both positive and negative numbers:

### Explaining formula in cell C6

#### Step 1 - Logical test

B3<0 becomes -1.2<0 returns TRUE.

#### Step 2 - Evaluate TRUE argument in IF function

The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.

Function syntax: IF(logical_test, [value_if_true], [value_if_false])

#### Step 3 - Convert negative value to positive

The ABS function converts negative numbers to positive numbers.

Function syntax: ABS(number)

ABS(B3) becomes ABS(-1.2) returns 1.2

#### Step 4 - Convert negative value to positive

MOD(ABS(B3), 1) becomes MOD(1.2, 1) and returns 0.2

#### Step 5 - Convert positive value to negative

-MOD(ABS(B3), 1) returns -1.2

## 11. MOD function won't work with really large numbers

There seems to be a problem with large numbers and Microsoft knows about it:

https://support.microsoft.com/kb/119083

The MOD() function returns the #NUM! error if the following condition is true:

('*divisor*' * 134217728) is less than or equal to '*number*'

### Useful links

MOD Function - Microsoft

Extracting Vectors From A Matrix

### 'MOD' function examples

Excelxor is such a great website for inspiration, I am really impressed by this post Which numbers add up to […]

I discussed the difference between permutations and combinations in my last post, today I want to talk about two kinds […]

What's on this page Reverse text Insert random characters Convert letters to numbers How to shuffle characters in the alphabet […]

### 'MOD' function examples

The quotient function returns the integer portion of a division. The remainder is left out. This function is useful in […]

(Chart data is made up) This article demonstrates two ways to color chart bars and chart columns based on their […]

This article demonstrates formulas that rearrange values in a cell range to a single column. Table of Contents Rearrange cells […]

### Functions in 'Math and trigonometry' category

The MOD function function is one of 63 functions in the 'Math and trigonometry' category.

### Excel function categories

### Excel categories

### 8 Responses to “How to use the MOD function”

### Leave a Reply

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Paste image link to your comment.

**Contact Oscar**

You can contact me through this contact form

Hello! Very interesting but the Highlighting every other group Example 1 upon the first column values doesn't actually work as expected. Please try to put i.e. 2 in A7. Any clue why?

It's rounding issue. To fix it just use INT function:

=MOD(INT(SUM(1/COUNTIF($A$2:$A2,$A$2:$A2))),2)

Example 1 would be more representative if we have textual values in column A instead of numbers. In the example as it is we can use simply MOD($A2,2). But with textual values it's much trickier, and the Example 2 is where Oscar's formula realy shows its power.

dopsz,

Very interesting but the Highlighting every other group Example 1 upon the first column values doesn't actually work as expected. Please try to put i.e. 2 in A7. Any clue why?You are right, I forgot to add that column A must be sorted.

Leonid,

Example 1 would be more representative if we have textual values in column A instead of numbers. In the example as it is we can use simply MOD($A2,2). But with textual values it's much trickier, and the Example 2 is where Oscar's formula really shows its power.Yes, bad example. I have changed values in column A.

[…] Mod function […]

[…] You can use the mod function inÂ a conditional formatting formula to highlight ever n-th row: Learn how the MOD function works […]

Hi Oscar,

Thank you for your amazing tricks. Learned a lot from it.

Suppose I need to highlight every other two consective rows:

ACCT Amount

ACCT1 DR 12 Shaded

ACCT2 CR -12 Shaded

ACCT1 DR 14

ACCT2 CR -14

ACCT1 DR 16 Shaded

ACCT2 CR -16 Shaded

ACCT1 DR 20

ACCT2 CR -20

Sanad

=MOD(ROW(A1),4)>=2

Thanks a lot Oscar. That was simple & elegant!