# How to use 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.

## Excel Function Syntax

MOD(*number*, *divisor*)

## Arguments

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). |

**What's on this page**

- 1. What is a remainder?
- 2. How to calculate a formula every n-th row
- 3. Highlight every n-th row?
- 4. Highlight every other group? Example 1
- 5. Highlight every other group? Example 2
- 6. How to return the fractional part of a number?
- 7. How to return the INTEGER portion of a division?
- 8. How to remove whole number and return decimal?
- 9. MOD function won't work with really large numbers?
- 10. Get Excel *.xlsx file

## 1. What is the remainder after a division?

The remainder is what is left after a division. If you divide 15 with 2 you get 7 and 1 is left over. 2*7 equals 14 and 15 minus 14 equals 1. 1 is the remainder or is left over. MOD(15,2) = 1. Sometimes nothing is left over, like 6 divided by 3 equals 2. The remainder is 0.

MOD(*number*, *divisor*) can be expressed like this: *number* - *divisor**INT(*number*/*divisor*)

### Example 1,

11 divided by 3 returns 2.

number - divisor*INT(number/divisor)

11-3*INT(11/3)

becomes

11-3*3

becomes

11-9 equals 2.

### Example 2,

Here is a different approach.

14 divided by 5 is 2 and 4/5

Take the remainder (4/5) and multiply it with the divisor (5)

5* (4/5) equals 4. 4 is the answer.

### Example 3,

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

**2. How to calculate a formula for every n-th 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

**3. Highlight every n-th row**

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.

**4. Highlight every other group, example 1**

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.

**5. Highlight every other group, example 2**

Example 2, groups based on values in column B, C and D.

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.

**6. How to return the fractional part of a number?**

If you use 1 as a divisor the MOD function returns the fraction of a number. *number* - INT(*number*/*divisor*)

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

**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.

## 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:

## 9. 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*'

## Recommended reading

### 'MOD' function examples

The following 13 articles have formulas containing the MOD function.

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

I found an old post that I think is interesting to write about today. Think of two overlapping ranges, it […]

This article demonstrates a formula that sorts items arranged horizontally based on the adjacent numbers, every other column contains a […]

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 […]

Question: How do I create a list of dates with blanks between each quarter? (Q1, Q2, Q3 and Q4) Answer: […]

Can someone explain to me why this happens? This example is working. FREQUENCY function returns {2; 1; 1}. 2 values (0.1 and […]

The quotient function returns the integer portion of a division. Example, 5/2 = 2.5. The integer is 2. Excel Function […]

This article explains how to build an array formula that sums numerical ranges. Example, I want to know how to […]

Color odd months Conditional formatting formula: =MOD(MONTH($B6),2) Explaining CF formula in cell B6 Step 1 - Calculate number of month The […]

Mark G asks in Create a random playlist in excel: Can this example be modified to create a true round-robin […]

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

The picture above shows data presented in only one column (column B), this happens sometimes when you get an undesired […]

### 'MOD' function examples

The following 2 articles have formulas containing the MOD function.

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

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

### Functions in 'Math and trigonometry' category

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

### 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!