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

### What's on this page

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

## 1. MOD Function Syntax

MOD(*number*, *divisor*)

## 2. MOD Function 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). |

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

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

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

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

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

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

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

## 10. 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 […]

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

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

### 'MOD' function examples

The quotient function returns the integer portion of a division. Example, 5/2 = 2.5. The integer is 2. What's on […]

(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 many 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!