In this blog post we explore 4 Excel MOD function examples. Hopefully this will help to see how versatile this function can be.

To start we better have a quick introduction to how to use the MOD function. It looks like this;

=MOD(number, divisor)

You need to provide it first with the number you want to divide, and then the number you want to divide it by (divisor).

You can see some basic examples of this below.

Ok, lets get on with some more impressive **real-world MOD function examples**.

Here is the video tutorial of the examples explained below.

Excel stores dates and times as a number. The time is the fractional part of that number.

For example, the date and time 06/07/2017 10:15 is stored in Excel as 42922.42708.

Now this does not mean anything to us, but to extract the time part only of .42708 we could use the MOD function and then format the result as time to make sense to us.

The MOD function would be used to divide the number (date and time value) by 1. As this would leave the fractional part as the remainder.

In the example below the following formula was used to extract the time result.

=MOD(A2,1)

This calculation was used in my tutorial to calculate the time elapsed in days, hours and minutes.

Another clever example of the MOD function may be to stop the entry of odd numbers in a range.

If a number is an even number, a remainder of 0 would be returned when divided by 2.

It stands to reason then that odd numbers would return a different remainder. We could use this understanding to insert the MOD function into a Data Validation rule.

Select the range of cells you want to apply the validation rule to. Click **Data** > **Data Validation**.

Select **Custom** from the Allow list and enter the formula below into the box provided.

=MOD(A2,2)=0

In this formula, cell A2 is the first cell of the range that you selected. This formula ensures that only values that return a remainder of 0 when divided by 2 are allowed.

In my online course on how to create automated sports league tables and tournaments in Excel, I have a section on calculating cricket statistics and league tables.

This example comes from that course. We have cells containing the number of overs bowled, but we want to see how many balls is this.

For those who don’t follow cricket. There are 6 balls per over bowled. So if a bowler has made 6.2 overs. They have done 6 overs of 6 balls each and then 2 more balls.

In the spreadsheet below the number of overs is in column B and then the following formula was used in C.

=INT(B3)*6+MOD(B3,1)*10

This formula used the INT function to extract the integer part of column B and multiplies it by 6 to calculate the total balls bowled.

Then the MOD function is used to extract the fractional part (.2 in B3) by dividing it by 1. This is then multiplied by 10 to convert 0.2 to 2. The two parts of the formula are then added together.

For the final example, we will use the MOD function to help us sum every third cell in a range. This formula can be adapted to sum every other cell, or every fifth cell, or whatever you may need.

This formula uses the impressive SUMPRODUCT function, a favourite of mine.

The formula below uses the ROW function to return the row number of the cell reference starting with B3. A 2 is then subtracted from this because the range starts from row 3. So row 3 is actually row 1 of the range.

The MOD function is then used to divide this number by 3. If the number is divisible by 3 then it must be a multiple of 3.

The number that meet this test in range B3:B11 are then summed.

=SUMPRODUCT((MOD(ROW($B$3:$B$11)-2,3)=0)*($B$3:$B$11))

Take this example where column A contains a start date and time, and column B an end date and time. We wish to **calculate the elapsed time in days, hours and minutes** e.g. 11 days 4 hours 9 minutes.

There are multiple ways of calculating date and time difference in Excel. In this scenario we will need to get a little clever.

As you may well know, date and time values are stored as numbers in Excel. For example, the 05/01/2017 10:10 is stored as 42740.42.

Therefore, if I write the formula as =B2-A2, then the result is returned as 2.993056.

To return a result that makes sense to us, we will tackle the date and time parts of the cell separately.

To work with just the date part of the cell, we will use the INT function. This function rounds a value down to the nearest integer.

So if we write the function as below. This will return only the integer part of the date difference, and this is the number of days.

=INT(B2-A2)

We now need to work on the number of hours and minutes, which is the decimal part.

To return only the decimal part of the B2-A2 formula, we will use the MOD function. This function returns the remainder after a number is divided by a divisor.

We will use it to divide the B2-A2 formula by 1 so that it returns to remainder as the decimal part.

We will then use the HOUR and MINUTE functions to return the hours and minutes from this decimal value.

So the formula below returns the number of hours elapsed.

=HOUR(MOD(B2-A2,1))

And this returns the number of minutes elapsed.

=MINUTE(MOD(B2-A2,1))

Finally, we need to put this altogether as one Excel formula. We can use the ampersand (&) to concatenate the different parts of the formula.

You can construct the result to look however you want. For example the formula below would return the result as 2 days 23 hours and 50 minutes.

=INT(B2-A2)&" Days "&HOUR(MOD(B2-A2,1))&" Hours "&MINUTE(MOD(B2-A2,1))& " Minutes "

And this formula would display the result as 2 days 23:50.

=INT(B2-A2)&" Days "&HOUR(MOD(B2-A2,1))&":"&MINUTE(MOD(B2-A2,1))

The spreadsheet below contains totals in every fifth row starting from row 3. We want to only add these sales totals.

The formula below is entered in cell E1.

Let’s break the formula down a little.

**ROW**: The ROW function is used to return the row number that the formula is checking. -2 has been entered on the end because the values begin from row 3. This -2 ensures that instead of using rows 7, 12, 17 and 22. Rows 5, 10, 15 and 20 are used.

**MOD**: The MOD function used to find the remainder after a number is divided by a divisor. It is used in this formula to check whether it is the fifth row or not. If it is the fifth row, after dividing the row number by five, the result will be 0.

Note: The MOD Function used in Excel VBA to check if a number is even.

**SUMPRODUCT**: The SUMPRODUCT function will perform the summing. The first array is the test for the row. This is what is returned;

{0;0;0;0;1;0;0;0;0;1;0;0;0;0;1;0;0;0;0;1}

The 1 is returned when the condition is true. This is multiplied by the range of values and then summed which gives us our total.

Note: See the SUMPRODUCT function used to count values based on multiple conditions.

I hope this explanation makes sense and you are able to adapt it for your own situation. If not, please check out this video.

Sum Every Nth Row in a List – Microsoft Excel Tips and Tricks

I accomplished this using the Mod operator. The Mod (modulus) operator is the VBA equivalent to the MOD function in Excel. It’s purpose is to find the remainder after a number has been divided by a divisor.

The Mod operator is written as below where *a* and *b* represent variables containing values;

*a *Mod *b*

If *a* stored the value 20 and *b* stored 6 then the result would be 2. And if *a* stored 20 and *b* stored 4 then the result would be 0.

To check if a value is an even or an odd number I used the Mod operator to divide the variable holding the number by 2. If the result returned 0 then it must be an even number. And if not then it must be an odd.

The end result was to use an If… Then… Else conditional statement to take action on the answer from the Mod operator. This looked like the below;

If *a* Mod 2 = 0 then

*The number is even, do something*

*Else*

*The number is odd, do something else*

End If

]]>